php - 如何将 javascript 变量传递给 PHP

标签 php javascript post

我正在创建一个动态表单,其中包含一个名为“添加更多行”的按钮,当单击此按钮时,JavaScript 函数会创建一行具有适当 ID 的新文本框。

问题是,我如何将计数器变量从我的 JavaScript 函数传递到我的下一个 php 页面,以便它现在知道有多少行文本框要接收 $_POST

我有我的 JavaScript 函数,但是我缺少它自己创建的行中的数据。

有什么想法吗?

谢谢

这是我的js函数

window.onload=function()
{

inp=document.getElementsByTagName('input');
for(c=0;c<inp.length;c++) 
    {
        if(inp[c].value=='add') 
        {
           inp[c].onclick=function() 
            {
                n=15;

               x=document.createElement('input');
               x.setAttribute('rows',1);
               x.setAttribute('cols',20);
               x.name='time'+n;
               document.getElementById('txtara').appendChild(x)
               x=document.createElement('input');
               x.setAttribute('rows',1);
               x.setAttribute('cols',20);
               x.name='event'+n;
               document.getElementById('txtara').appendChild(x)
               x=document.createElement('input');
               x.setAttribute('rows',1);
               x.setAttribute('cols',20);
               x.name='supplies'+n;
               document.getElementById('txtara').appendChild(x)

            var sel = document.createElement('select');



        y = document.createElement('option');
        y.value = 'Yes';
        y.name = 'success' + n;
        y.innerHTML = y.value;

        x = document.createElement('option');
        x.value = 'No';
        x.name = 'success' + n;
        x.innerHTML = x.value;


        sel.appendChild(y);
        sel.appendChild(x);


        document.getElementById('txtara').appendChild(sel);
        document.getElementById('txtara').appendChild(sel);
        document.getElementById('txtara').appendChild(sel);


               x=document.createElement('input');
               x.setAttribute('rows',1);
               x.setAttribute('cols',20);
               x.name='comment'+n;
               document.getElementById('txtara').appendChild(x)

               document.getElementById ('txtara').innerHTML += '<br>';


        n++;

           }
         }

    }
    //-->
}

最佳答案

你应该添加一个 <input type="hidden" name="num_rows" value="0">添加到您的表单并将其值更新为提交表单时的行数。

关于php - 如何将 javascript 变量传递给 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6564668/

相关文章:

javascript - ajax请求全部返回错误500

php - Python Post - PHP 请求

apache-flex - 弹性 : Send HTTP POST request with binary data in body

php - 突出显示从现在到 2 周后的所有日期

php - 使用 BINARY 从 MYSQL 到 SQLSRV 的密码区分大小写

PHP 如何在 foreach 循环期间而不是在结束时进行回显?

javascript - 单击抽屉导航 Bootstrap 时不显示侧面导航栏

javascript - 通过jquery调用其他页面

javascript - 是否有可能知道脚本是如何加载的

javascript - 使用 Javascript 将自定义查询字符串变量拉入隐藏表单字段