I’m making a simple cart program in php.
What the code below does is to update the current page, so that the quantity for the product that is bought will be zeroed. And then I tried to link it with another page, which performs another action(to update the database).
What can I do with this experimental code that I made up:
echo "<td>
<a href=\"$_SERVER[PHP_SELF]?action=zeroline&id=$product_id&commitbuy.php?ids=$id&qoh=$qtyhand&qtb=$quantity\">
<img src=\"http://localhost/onlinestore/img/system/accept-icon.png\"></img></a>
</td>";
The part which updates the current page seems to be working but, the part that updates the database is not working.
As you can see I just link those 2(PHP_SELF, which is viewcart.php and then commitbuy.php) together with an ampersand.
Should I just place the code that updates in the viewcart.php?
Whats the proper way of doing this.
Yeah, unless you’re using Ajax to send two different requests to the server simultaneously (and for a good reason) you should handle all the associated business logic for an action in the backend PHP. Even with Ajax, I can’t imagine that logic needing to be more than a single request that updates the quantity to zero in the interface using Javascript/jQuery on success.
As mentioned by @martin earlier, you don’t want to trigger a second dependent action before you’re confident the first action was actually successful.