Hey
I’ve got this webshop code, where a for loop checks the MySQL table and generates forms for each product:
while ($row = mysqli_fetch_array($data)){
array_push($product, array($row['kaup'], $row['hind'], $row['yhik']));
// generate unique submit "buttons" by concatenating an index $rowcount to each name of submit
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">
<table>
<tr> <th>Product:</th> <td>'.$product[$rowcount][0].'</td> </tr>
<tr> <th>Price:</th> <td>'.$product[$rowcount][1].' EUR</td> </tr>
<tr> <th>Quantity:</th> <td><input type="text" name="quantity" /></td> <td>'.$product[$rowcount][2].'</td> </tr>
<tr> <td><input type="submit" name="submit'.$rowcount.'" value="Add to cart"></td></tr>
</table>
</form>';
$rowcount++;
I generate unique submit buttons for each form by concatenating a number to it.
Now what I do is, I try to listen for these submits.
But it seems that this code runs only once, at start or there is smth else wrong.
I thought of one solution, if I could make a separate file for this and set action like formlistener.php, so it would run every time it is triggered.
But it would clutter the code. And $_SERVER[‘PHP_SELF’] should do just the same? How could this be solved?
for ($i = 0; $i <= $rowcount; $i++) {
// If this submit is set, then push the data into Session variable order
if (isset($_POST['submit'.$i.'']) && is_numeric($_POST['kogus'])) {
$quantity = trim($_POST['quantity']);
array_push($_SESSION['order'], array($product[$i][0], $product[$i][1], $product[$i][2], $quantity));
}
}
Thanks