0

I have this codes that create a textbox, and each textbox has it's own unique id eg textbox1, textbox2 so on. I had trouble in submitting the data because the textbox is dynamic, u will not know how many textbox created by the user., so I dont have idea on how to post the data eg., $_POST['textbox'],.

$(document).ready(function(){

    var counter = 2;

    $("#addButton").click(function () {

    if(counter>10){
            alert("Only 10 textboxes allow");
            return false;
    }   

    var newTextBoxDiv = $(document.createElement('div'))
         .attr("id", 'TextBoxDiv' + counter);

    newTextBoxDiv.after().html('<input type="text" name="txtLine' + counter + '" id="txtLine' + counter + '" placeholder="Line#' + counter +' " >');

    newTextBoxDiv.appendTo("#TextBoxesGroup");


    counter++;
     });

1 Answer 1

2

You can submit the form elements with square brackets like so and it will post all of the form inputs as an array:

<form method="post" action="form.php">
  <input name="txtLine[]" />
  <input name="txtLine[]" />
  <input name="txtLine[]" />
  <input type="submit" />
</form>

You can then access all of them in an array like so $_POST["txtLine"]

Output of $_POST:

Array
(
    [txtLine] => Array
        (
            [0] => hey
            [1] => there
            [2] => jack
        )

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

3 Comments

so u mean,instead of using counter to create unique ID,.i should use []?
You can still use the counter to create a unique ID for the HTML id or class name, but for the name you can use the square brackets and that will be accessible as an array on the PHP side.
Thanks,. In the file where in it post the data.. I added these codes $text = $_POST['text']; if (isset($_POST['submit'])) { echo count($text); foreach ($text as $txt) { echo $txt. ' '; } } HOPE IT HELPS ... thx

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.