0

I'm new to php. I have a jQuery script that will generate new elements. The user clicks submit and is taken to page2.php. That's where I need a php loop to determine how many text inputs the user created.

page1.php

<!DOCTYPE html>
<html>
<head>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.9.1.min.js'>   </script>
<script type='text/javascript'>
$(document).ready(function() {
    $('#btn').click(function() {
        var x = parseInt($('#hidtxt').val()) + (1*1);
        var z = "<input type='text' id='txt" + x + "' name='txt" + x + "' size='10'/>";
        $('#main').append(z);
        $('#hidtxt').val(x);
    });
});
</script>
</head>
<body>
  <form name='myform' action='page2.php' method='post'/>
    <input type='hidden' value='1' name='hidtxt' id='hidtxt'/>
    <div id='main'>
      <input type='text' id='txt1' name='txt1' size='10'/>
    </div>
    <input type='button' id='btn' name='btn' value='Add Text Box'/>
    <input type='submit' value='Submit' id='subBtn' name='subBtn'/>
  </form>
</body>
</html>

Then page2.php

<?php
    //CREATE LOOP TO POST ALL TEXT INPUTS
    $val1 = $_POST['txt1'];
    $val2 = $_POST['txt2'];
    ....
    ....
    $valx = $_POST['txtx'];


?>

3 Answers 3

5

Give the textbox name as an array like

<input type='text' id='txt1' name='txt[]' size='10'/>

And then get it like

print_r($_POST['txt']);

Then you will get the array of the textbox values.Orelse you can use loop like

for($i=0;$i<$cnt;$i++) {        
    $val[$i] = $_POST['txt'][$i];
}

Here you need to get the count of total input fields that you have given for the loop as $cnt.Maintain an hiddenfield with initial value as 1 and Whenever you click on Add text box button you need to increment it,and after submitting the form you can get the $cnt value as the number of input fields which is in the hidden field.

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

3 Comments

So this passes each element into an array. How do I call a specific value from the array?
If you want to call the specific input then you can use like $_POST['txt'.$id_of_input]
sweet. This is perfect. Thank you
2

Count the number of inputs with Jquery in the first page create a hidden input field and then put that value in the hidden field . In the next page read that using PHP .

Comments

1

One way would be to have a hidden text field inside the same form. This will contain the number of input elements that have been generated (simple plain JavaScript can be used for this). So on the 2nd php page, you first find out how many elements were generated, and then use a for loop to get their values into an array which you can parse as desired. (Typing this from my phone, so unable to post code sample. But should be easy enough.)

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.