简体   繁体   English

如何单击 HTML 表格行并将行的数据发送到 PHP(作为 POST)?

[英]How to click on HTML table row and send row's data to PHP (as POST)?

I need to make a table with clickable rows (for this I found some Javascript solutions).我需要制作一个带有可点击行的表格(为此我找到了一些 Javascript 解决方案)。 When I click one row, a form will be filled with data from the clicked row.当我单击一行时,表单将填充来自单击行的数据。

But I don't know how to send the data from HTML table to PHP, when I click on that row.但是当我单击该行时,我不知道如何将数据从 HTML 表发送到 PHP。 Data must be sent as POST, as I mentioned in title.正如我在标题中提到的,数据必须作为 POST 发送。

I have no idea how to start, so I cannot show you nothing.我不知道如何开始,所以我不能向你展示任何东西。 Using multiple forms inside one table is not allowed, <a> tag around <td> is not allowed either, so I'm stuck.不允许在一个表内使用多个表单,也不允许在<td>周围使用<a>标签,所以我被卡住了。 From what I searched on web, the solution would be AJAX, but I'm not familiarized with it, never used AJAX, so if possible, a non-AJAX solution would be much appreciated.从我在网上搜索的内容来看,解决方案是 AJAX,但我不熟悉它,从未使用过 AJAX,因此,如果可能,非常感谢非 AJAX 解决方案。 If not, please show me a functional example.如果没有,请给我看一个功能示例。

EDIT solved编辑解决

In the end I did it without any AJAX, thanks to ideas from emartel and Guerra .最后,我在没有任何 AJAX 的情况下做到了,感谢来自emartelGuerra 的想法。 I made a single form with a hidden input field and I submitted it programatically, after I collected the required data from the row I clicked on.我制作了一个带有隐藏输入字段的表单,并在从我单击的行中收集了所需的数据后以编程方式提交了它。

I echoed the tr tags like this: <tr data-id=$id_from_db onclick="submit_id(this)"> .我回应了这样的 tr 标签: <tr data-id=$id_from_db onclick="submit_id(this)">

function submit_id(tableRow) {
        //get ID
        var myID = tableRow.dataset.id;

        //set ID
        var setID       = document.getElementById('order_id');
            setID.value = myID;

        //submit
        var form = document.getElementById('myform');
        form.submit();
    };

PS I tried to embed some PHP code, but I failed, it didn't shown on page. PS 我试图嵌入一些 PHP 代码,但我失败了,它没有显示在页面上。

After filling your form ( name="myform" and method="post" ) from JavaScript, simply call:从 JavaScript 填写表单( name="myform"method="post" )后,只需调用:

document.myform.submit();

This should do the work这应该做的工作

You can put some kind of input hidden on each row with an ID.您可以将某种输入隐藏在带有 ID 的每一行上。 So you can use just 1 form passing the ID to submit post and recovery the entire register on server.因此,您可以仅使用 1 个传递 ID 的表单来提交帖子并恢复服务器上的整个注册表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在HTML表格的可点击行中发送数据 - How to send data in a clickable row of an html table PHP、HTML 和 SQL:如何获取表行号并作为参数发送 - PHP, HTML and SQL: How to get table row number and send as parameter 如何在PHP中单击鼠标从html表中检索行? - How to retrieve row from a html table on mouse click in PHP? 如何使用html表单和php连续两次发布数据 - How to post data two times in a row with html forms and php 如何在单击现有的 html 表格行时显示隐藏的表格行? - How to show hidden table row on click of existing html table row? 如何在单击表行时将数据从 PHP 中的表行传递到另一个页面 - how to pass data from table row in PHP to another page on click the table row 单击表行时,如何执行Ajax请求以将表行列数据解析为PHP? - How to do an Ajax request to parse table row column data to PHP on click of table row? 将行添加到表并发送到服务器,通过jquery将表发布到php - Add row to table and send to server, post table via jquery to php 单击表格行以在新的php页面中显示该选定行的数据 - Click a table row to display the data of that chosen row in a new php page 如何使用复选框(PHP)从动态表中选择一行并插入这些行以发送邮件功能 - How to select a row(s) from dynamic table using checkbox (PHP) and insert those row to send mail function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM