使用php对html表中的行进行计数 [英] counting the rows in html table using php
问题描述
我有一个动态的HTML表格,允许用户点击按钮添加行。来自每行的数据需要插入MySQL表格中,我使用PHP脚本。现在,因为每次HTML表格中的行数可能不同,我该如何在PHP中处理这个问题。一种方法是计算HTML表格中的行数(我发现某处可以这样做)
$ p $ $ rows = $表 - >找到( '#studenttable'); / *我的代码第4行* /
$ count = count($ rows); / * studenttable是我的表的ID * /
echo $ count;
,然后运行for循环插入每行的数据。但是,这是一个致命的错误。
注意:未定义的变量:第4行的savedata.php中的表
致命错误:在第4行的savedata.php中的非对象上调用成员函数find()
另一种方法可能是使用foreach循环,我完全不知道如何在这种情况下实现。
此代码动态添加新行
var count = 2;
函数addRow()
{
var table = document.getElementById(studenttable);
var row = table.insertRow(-1);
var cell15 = row.insertCell(14);
var ipt8 = document.createElement('input');
ipt8.setAttribute(type,hidden);
ipt8.name =htmlrow [];
ipt8.value = count;
cell15.appendChild(ipt8);
count ++;
}
获取行数的PHP文件
<?php
$ arr = $ _POST ['htmlrow'];
foreach($ arr as $ val){
$ count = $ val;
echo $ count;
}
?>
仍然无法获得结果。
您无法直接访问PHP中的HTML元素。
我的建议会改变客户端代码所以每当一行被添加到表中时,一个< input type =hiddenvalue =value-of-rowname =htmlrow []/>
被添加到表单中。 这样,当提交表单时(你把所有的东西包装在表单中,对吗?),你可以访问在PHP中使用 $ _ POST ['htmlrow']
>处理表单中的表格行,该表格现在将包含用户数据数组。
I have got a dynamic HTML table that allows the user to add rows on click of a button. The data from each row needs to be inserted in MySQL table for which i use the PHP script. Now since each time the number of rows in the HTML table may be different, how do I deal with this in PHP. One way is to count the number of rows in HTML table (which I found somewhere to be done like this)
$rows = $table->find('#studenttable'); /*line 4 of my code*/
$count = count($rows); /*studenttable is the id of my table*/
echo $count;
and then run a for loop for inserting the data for each row. But that is giving a fatal error.
Notice: Undefined variable: table in savedata.php on line 4
Fatal error: Call to a member function find() on a non-object in savedata.php on line 4
The other way round may be to use a foreach loop which I am completely not getting how to implement in this situation.
This code dynamically adds new rows
var count=2;
function addRow()
{
var table=document.getElementById("studenttable");
var row=table.insertRow(-1);
var cell15=row.insertCell(14);
var ipt8 = document.createElement('input');
ipt8.setAttribute("type", "hidden");
ipt8.name = "htmlrow[]";
ipt8.value = count;
cell15.appendChild(ipt8);
count++;
}
PHP file to get the number of rows
<?php
$arr = $_POST['htmlrow'];
foreach ($arr as $val) {
$count= $val;
echo $count;
}
?>
still not getting result.
You cannot directly access HTML elements in PHP.
My suggestion would be altering the client-side code so that whenever a row gets added to the table, an <input type="hidden" value="value-of-that-row" name="htmlrow[]"/>
gets added to the form.
This way, when submitting the form (you have the whole thing wrapped inside a form, right?) you can access the generated table rows in the PHP handling the form using $_POST['htmlrow']
, which will now contain an array of the user data.
这篇关于使用php对html表中的行进行计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!