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条)

报告相同问题?