开发者

Creating a table with each row containing a button that relates to the data of that row

I was wondering how I would be able to create a button that would be related to the information in 开发者_如何学编程that row.

So far I have an array that contains a person's first name, last name, and phone number. I would also like to create a button, so when the user clicks that the information of the person is carried to the next PHP page.

Should I be creating a form each time I create a row in the array?

So far I have:

foreach($array as $row)
        {
                echo (
                "<tr>".
                "<td>".$row['last_name'].       "</td>".
                "<td>".$row['first_name'].  "</td>".
                "<td>".$row['phone_no'].        "</td>".
                "<td>".$row['date_of_birth'].   "</td>".
                "<td>".$row['membership'].  "</td>".
                "<td><Button>Reserve</Button></td>".
                "</tr></table>");
        }

The reason it is being echoed is because I am using it in an ajax function and wish to output the result that way.

Any suggestions?


You can use the new HTML5 data attribute for this:

<td><Button data-id='4'>Reserve</Button></td>

You can then use javascript, or more specifically jQuery's .data() functionality to get this data value

Edit

<!-- Firstly give your button a class -->
<td><button class='ajax_button' data-id='4'>Reserve</button></td>

Then the Javascript

// On click for a button
$(".ajax_button").click(function(){

     // Get the id data attribute
     var id = $(this).data("id");
});


Html forms http://www.w3schools.com/html/html_forms.asp

you only need one form, with many inputs. if you want to do multiple data in the form you can just give it a name like name='data[{n}][last_name]' where {n} is an incremented number or the id of the row.

you will then have an array like $_POST['data'][{n}][<fields>] when you submit it

you also dont need the braces around your data to be echo'ed. echo '<stuff>'; is fine

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜