user2901740
user2901740

Reputation: 1571

Get Values from dynamic textbox?

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

Upvotes: 0

Views: 1715

Answers (1)

Mingle
Mingle

Reputation: 836

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
        )

)

Upvotes: 2

Related Questions