I have the following form: (please note I am aware that there are no validation in my code)
<form name="frmsplitter" method="POST" enctype="multipart/form-data" action = 'step2.php'>
<table>
<thead>
<tr>
<select style="width: 80%" name="cobbulkamount">
<option>please select a bulk item</option>
<option value= 100>100</option>
<option value= 200>200</option>
</select>
</tr>
</thead>
<tbody>
<tr>
<td><label>Select number of items you want the bulk amount to be splited : </label><input type="text" name="txtnumberofitems" value="<?php if (isset($_POST['btnsplitItem'])){echo $_POST['txtnumberofitems'];}?>" /></td>
<td></td>
</tr>
<tr>
<td><input type="submit" value="submit" name="btnsplitItem" /></td>
</tr>
</tbody>
</table>
</form>`
the form submits it goes to the following page :
<?php
if (!empty($_POST['txtnumberofitems'])) {
$bulkvalue = 200;
$numberofitems = $_POST['txtnumberofitems'];
echo "<form name='frmsplitter' method='POST' action = 'step3.php'>";
echo "amount to be splited <input type='text' name='txtamountof' value =$numberofitems> ";
for ($index = 0; $index < $numberofitems; $index++) {
echo "<table>".
"<tr>
<td>|amount: <input type='text' name='txtamount.$index'/></td>
<td>|Reference 2: <input type='text' name='txtref.$index'/></td>
<td>|DCIP:<select name='cobdcip'.$index>
<option>DR</option>
<option>CR</option>
</select></td>
</tr>
</table>";
}
echo "<br>";
echo "<input type='submit' name='btnpopulate' />";
echo "</form>";
}
?>`
the form submits it goes to the following page :
if (isset($_POST['btnpopulate'])) {
$number = (int)$_POST['txtamountof'];
for ($index = 0; $index < $number; $index++) {
if (!empty($_POST['txtamount'.$index]))
{
echo 'nooooooooooo';
}
else {
echo $_POST['txtamount'.$index];
echo '<br>';
}
}
} `
My problem is that I get the Notice: Undefined index: txtamount0 and Notice: Undefined index: txtamount1
The name of your input is : txtamount.1
, not txtamount1
, so you should access it this way :
for ($index = 0; $index < $number; $index++) {
if (array_key_exists('txtamount.'.$index, $_POST)) {
[...]
Or change your html :
<td>|amount: <input type='text' name='txtamount$index'/></td>