0

What i want to do is,

I have a code which generates 2 textboxes on a button click event througn ajax as many time as button clicks. So On the click of submit button how can i identify textboxes and getvalue of each textbox.

So I want to give dynamic name to each text box.and on the form submit i want to fetch each textbox's value.

can i do something like this

<input type="text" name="fname[]" value="Hello" />
<input type="text" name="fname[]" value="World" />

and get value on submit

<?php
if(isset($_POST['txtfname[]'])){
    echo $_POST['txtfname[0]'];

}
?>

Can anybody help?

Nirali Joshi
  • 1,968
  • 6
  • 30
  • 50

4 Answers4

1

The values are in the $_POST['fname']-array.

Meaning:

$_POST['fname'][0] --> "Hello"

$_POST['fname'][1] --> "World"

Marty McVry
  • 2,838
  • 1
  • 17
  • 23
0

While creating the text box as you said by a ajax give the name of the newly created button as an array as you said.And after submitting the form if you print_r($_POST) you could see the posted array which contains all the values of the elements submitted in the form.

웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
0

This might be possible, but following is probably a better way to do it. If you generate each of the inputs so their names are in this format: <input type="text" name="fname_0" value="value" /> where 0 is the number, you can then use this code to loop through each one and echo the value:

$prefix = "fname_";
foreach ($_POST as $post) {
    if (strncmp($post, $prefix, strlen($prefix))) {
        echo $post;
    }
}

Edit
Alternatively, if you format the inputs like <input type="text" name="fname" value="value" />, they will be accessible through $_POST['fname'] as an array, so you can echo each one like this (in your PHP code):

foreach ($_POST['fname'] as $fname) {
    echo $fname;
}
tomb
  • 1,817
  • 4
  • 21
  • 40
0

In your HTML for each of your elements put

<input type="text" name="fname[]" value="value" />

Where value is the value of each textbox. This will put all of the values of each textbox inside a single array. In the code to dump the values of all the inputs, you can put:

<?php
if(isset($_POST['fname'])){
    $array =  $_POST['fname'];
    var_dump($array)

}
?>
Adrian Toman
  • 11,316
  • 5
  • 48
  • 62
premananth
  • 179
  • 5