dongsi5381 2012-09-12 04:49
浏览 36
已采纳

如何在提交后将变量从javascript传递给php

I have a script add rows to the table with textfields everytime i click add row:

<script language = "javascript">

var x = 0;

function addRow(){

    var tbody = document.getElementById(tabID).getElementsByTagName("tbody")[0];
    var row = document.createElement("tr")
    var data1 = document.createElement("td")    
    var data2 = document.createElement("td") 

    var element = document.createElement("input");
    element.setAttribute("type", type);
    element.setAttribute("name", type);
    element.setAttribute("id", "Name"+x);
    element.setAttribute("style","width:95px");

    var element1 = document.createElement("input");
    element1.setAttribute("type", type);
    element1.setAttribute("name", type);
    element1.setAttribute("id", "Address"+x);
    element1.setAttribute("style","width:95px");

    var foo = document.tbody;

    data1.appendChild(element)
    data2.appendChild(element1)

    row.appendChild(data1)
    row.appendChild(data2)
    tbody.appendChild(row)

    x++;
}
</script>

How can I pass to php the value of my variable x after submitting, so I can count how many loop should I do?

  • 写回答

3条回答 默认 最新

  • doutong1890 2012-09-12 04:54
    关注

    You need to create an input hidden field inside your form and initialize it with the value of the counter . you can use javascript as follows

     var input = document.createElement("input");
     input.setAttribute("type", "hidden");
     input.setAttribute("name", "counter");
     input.setAttribute("value", x);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 经gamit解算的cors站数据再经globk网平差得到的坐标做形变分析
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式