0

I am dynamically creating text box by clicking on add button.In this I am incrementing a count variable when add button is clicked.How can I get this count value in my PHP file.I have tried window.location but its not working.My code is:

<script>
  var counter=1;
  function generateRow() {
   var count=counter;

    var temp ="<div class='_25'>Answer:<input type='textbox' size='100' id='textbox' name='mytext[]"+counter+"' placeholder='Please enter your answer text here.....'></input><input name='' type='button' value='Delete' /></div>";

    var newdiv = document.createElement('div');
    newdiv.innerHTML = temp + count;

    var yourDiv = document.getElementById('div');

    yourDiv.appendChild(newdiv);

    var js_var=counter++;

    xmlhttp.open("GET","tt.php?js_var="+js_var,true);
    xmlhttp.send(); 
 }
</script>
4
  • You are trying to pass variable from JS to PHP in same page? PHP is executed first and then JS, that do not know about PHP existence at all Commented Feb 17, 2014 at 11:10
  • are you passing the count value in tt.php page? Commented Feb 17, 2014 at 11:13
  • Can't you just count received inputs in your submit script? Commented Feb 17, 2014 at 11:14
  • @jogesh_pi-initially in this code I am not passing but later I tried but its not working. Commented Feb 18, 2014 at 6:09

3 Answers 3

2

in php file

$count_val=$_GET['js_var'];
0

Since you are adding the variable to the url you could get it with PHP's $_GET method.

$_GET['js_var'];

3
  • How is is "not working". Can you update your answer with the new code? Commented Feb 18, 2014 at 6:43
  • -Are you talking about same page or different page? Commented Feb 18, 2014 at 6:55
  • add it in tt.php to get the variable Commented Feb 18, 2014 at 13:33
0

Achieve it like this:

  var counter=1;
  function generateRow() {
   var count=counter;

    var temp ="<div class='_25'>Answer:<input type='textbox' size='100' id='textbox' name='mytext[]"+counter+"' placeholder='Please enter your answer text here.....'></input><input name='' type='button' value='Delete' /></div>";

    var newdiv = document.createElement('div');
    newdiv.innerHTML = temp + count;

    var yourDiv = document.getElementById('div');

    yourDiv.appendChild(newdiv);

    var js_var=counter++;

    // Update your counter Stat in input field
    $('#counter_stat').val( js_var );

    xmlhttp.open("GET","tt.php?js_var="+js_var,true);
    xmlhttp.send(); 
 }

Create a HTML Input tag for your counter value:

// Pass this field's value to PHP
<input type="hidden" name="counter_stat" id="counter_stat" value="1">

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.