[英]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 的情况下做到了,感谢来自emartel和Guerra 的想法。 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.