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?
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);
Add <input type="hidden" name="count_rows" value="0">
to your form, update its value to the row count when the form is submitted.
As suggested by Vinay already. Make use of name and value attributes of input elements. You can try out following code snippet.
<?php
if(isset($_POST['submit']))
{
print_r($_POST);
$counter= $_POST['counter'];
print_r("counter value : <b>". $counter. "</b>");
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="counter" value="5"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>