How to listen to multiple submits?

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].'&nbsp;&nbsp;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

Thank you for the solution.:slight_smile:

Just do nothing, have a simple form and one submit form and also have a hidden field inside the form to store the unique id (product id or SKU or something) retrieved from database.


while ($row = mysqli_fetch_array($data)){ 
    // 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].'&nbsp;&nbsp;EUR</td> </tr> 
          <tr> <th>Quantity:</th> <td><input type="text" name="quantity" /></td> <td>'.$product[$rowcount][2].'</td> </tr> 
          <tr> <td>
          <input type="hidden" name="product_id" id="product_id" value="'.$product[$rowcount][0].'" />
          <input type="submit" name="submit" value="Add to cart">
          </td></tr>
          </table> 
          </form>'; 
    $rowcount++;
}

And in your script after submit:


if($_SERVER['REQUEST_METHOD'] == 'POST'){// check if the form was posted.
    $product_id = trim($_POST['product_id']);
    $quantity = trim($_POST['quantity']);
    // now you can retrieve the other information of that particular product id
    
    // and store the information to your session
    array_push($_SESSION['order'], array($product[$i][0], $product[$i][1], $product[$i][2], $quantity)); 
}

Hope you understand.