1

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?

2
  • TRY document.getElementByID('divID').innerHTML = x Commented Sep 12, 2012 at 4:50
  • add it as a hidden form element on the submit Commented Sep 12, 2012 at 4:51

3 Answers 3

5

Add <input type="hidden" name="count_rows" value="0"> to your form, update its value to the row count when the form is submitted.

Sign up to request clarification or add additional context in comments.

2 Comments

how can i get the value of it in php?
You can use $_POST, $_GET, $_REQUEST variable to get value of element.
2

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

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.