提交表单传递的Javascript变量到PHP

问题描述:

这个问题从我的previous问题counting使用PHP 在HTML表中的行。 因为我没有工作的解决方案,我想尝试一种新的方式,但不知道如何实现它。 我分配唯一的ID添加的每个动态记录使用Javascript。因此,在JavaScript中计数变量存储的行present以HTML表格的数量。当我提交表单,它是由 savedata.php 处理。是否可以自动通过JavaScript变量计数 savedata.php 每个表单提交的时间。

This question follows from my previous question counting the rows in html table using php. Since I got no working solution, I want to try a new way but dont know how to implement it. I assign unique ID to each dynamic row added using Javascript. So the count variable in Javascript stores the number of rows present in HTML table. When i submit the form, it is processed by savedata.php. Is it possible to automatically pass the Javascript variable count to savedata.php each time the form is submitted.

下面是用于创建唯一ID的动态创建行元素的短版我的JavaScript文件。

Following is a short version of my Javascript file that is used to create unique IDs for elements of dynamically created 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.id = "text" + count; 
    ipt8.name = "htmlrow[]";      
    ipt8.value = count;        
    cell15.appendChild(ipt8);
count++;
}

新行被添加每次计数的增量,所以才将它传递到PHP文件将允许获取HTML表中的行数。我想每一个表单提交的时候自动进行。

Each time a new row is added, count increments, so just passing it to the PHP file will allow to get the number of rows in HTML table. I want to do it automatically each time the form is submitted.

将此表单里面。

<input name="NumRows" id="NumRows" type="hidden" value="0"/>

再修改JS来更新值...

And then modify your JS to update the value...

document.getElementById("NumRows").value = count;

然后,在你的PHP你可以做...

Then, in your PHP you can do...

$NumRows = $_REQUEST['NumRows'];

请注意,如果由于某种原因, numRows行不通过的形式(这里不可能的,但在其他地方可能),那么你应该在PHP中做到这一点......

Note that if for some reason NumRows isn't passed with the form (unlikely here but possible elsewhere) then you should do this in PHP...

$NumRows = isset($_REQUEST['NumRows'])?$_REQUEST['NumRows']:0;

这意味着如果 $ _ REQUEST ['numRows行'] 设置,使用它,否则,置 $ numRows行 0

Which means "If $_REQUEST['NumRows'] is set, use it, otherwise set $NumRows to 0"