I have a form with multiple textboxes which are created dynamically, now all these textboxes are of same name lets say txt
, now is there any way that when form proc
You have to name your textboxes txt[]
so PHP creates a numerically indexed array for you:
<?php
// $_POST['txt'][0] will be your first textbox
// $_POST['txt'][1] will be your second textbox
// etc.
var_dump( $_POST['txt'] );
// or
foreach ( $_POST['txt'] as $key => $value )
{
echo 'Textbox #'.htmlentities($key).' has this value: ';
echo htmlentities($value);
}
?>
Otherwise the last textbox' value will overwrite all other values!
You could also create associative arrays:
<input type="text" name="txt[numberOne]" />
<input type="text" name="txt[numberTwo]" />
<!-- etc -->
But then you have to take care of the names yourself instead of letting PHP doing it.
Create your text box with names txt[]
<input type='text' name='txt[]'>
And in PHP read them as
$alTxt= $_POST['txt'];
$N = count($alTxt);
for($i=0; $i < $N; $i++)
{
echo($alTxt[$i]);
}
If you want name, you could name the input with txt[name1]
, then you could get it value from $_POST['txt']['name1']
. $_POST['txt']
will be an associative array.