【问题标题】:Creating a table with each row containing a button that relates to the data of that row创建一个表,每行包含一个与该行数据相关的按钮
【发布时间】:2011-08-02 21:12:39
【问题描述】:

我想知道如何创建一个与该行中的信息相关的按钮。

到目前为止,我有一个数组,其中包含一个人的名字、姓氏和电话号码。我还想创建一个按钮,这样当用户点击时,人的信息就会被带到下一个PHP页面。

是否应该在每次在数组中创建一行时创建一个表单?

到目前为止,我有:

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>");
        }

它被回显的原因是因为我在 ajax 函数中使用它并希望以这种方式输出结果。

有什么建议吗?

【问题讨论】:

    标签: php html ajax html-table


    【解决方案1】:

    您可以为此使用新的 HTML5 数据属性:

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

    然后您可以使用 javascript,或者更具体地说是 jQuery's .data() functionality 来获取此数据值

    编辑

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

    然后是Javascript

    // On click for a button
    $(".ajax_button").click(function(){
    
         // Get the id data attribute
         var id = $(this).data("id");
    });
    

    【讨论】:

    • 非常感谢!今天学到了新东西!
    【解决方案2】:

    HTML 表单http://www.w3schools.com/html/html_forms.asp

    您只需要一个包含许多输入的表单。如果您想在表单中处理多个数据,您可以给它一个名称,如 name='data[{n}][last_name]' 其中 {n} 是递增的数字或行的 id。

    然后,当您提交时,您将拥有一个类似 $_POST['data'][{n}][&lt;fields&gt;] 的数组

    您也不需要在数据周围使用大括号来回显。 echo '&lt;stuff&gt;'; 很好

    【讨论】:

      猜你喜欢
      • 2010-09-24
      • 2015-05-18
      • 2014-10-30
      • 1970-01-01
      • 2017-10-05
      • 1970-01-01
      • 2021-09-09
      • 2019-04-18
      • 1970-01-01
      相关资源
      最近更新 更多