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.
You should look at the logs for your web server, you'll see that only one call is being made, and it contains the whole href
.
The ampersand is used to separate query string parameters in a URL, not to separate multiple URLs, so it all gets sent from the client in one go.
There's at least two reasons why you'd not want two calls: user experience - it might take longer to make two calls, and reliability - what if the database update fails?
Do you know of the model-view-controller MVC model?
精彩评论