Creating an order cart - How to header(refresh current page)
I am creating a order cart for a table of products.
Each product is displayed along with an "Order" button.
This is the code relating to the button:
<form action="" method="post">
<div>
<input type="hidden" name="id" value="' . $prId . '" />
<input type="submit" name="action" value="Order" />
</div>
</f开发者_运维百科orm>
This is the code relating to the buttons functionality:
if (isset($_POST['action']) and $_POST['action'] == 'Order')
{
// Add item to the end of the $_SESSION['order'] array
$_SESSION['order'][] = $_POST['id'];
header('Location: .');
exit();
}
In order for a user to see a product, the user must enter a search query, and if the query matches text in the products description, the result is displayed.
As you can see, when the user selects order, on the appropriate item, the id of the item is sent to the header and the page is directed to the order systems index.
What I would like to happen, is that when a user selects order, on the appropriate item. The page should be refreshed with the current search term still in effect.
Here is the code relating to the search form:
<form name="search" method="post" action="'.$_SERVER['PHP_SELF'].'">
<div>
Search For Product: <input type="text" name="find" />
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</div>
</form>
If anyone could provide me with some input on this one, I would really appreciate it!
Thanks!
With the order button, you would have to add a hidden field containing the old search query $_POST['find']
. After adding the item to the session variable, you redirect back to the search page. In your redirect, you will have to do something like: header('Location: ./?find=' . $_POST['find'])
. This will let you still have the same search on the search page (although you will have to account for the fact that you could get a $_GET or $_POST value for find).
You could also turn the functionality of the buttons into AJAX, and this would likely better in the long run. This way, you don't even have to reload or navigate away from the search page. There are plenty of ways to do AJAX. You can do it with native Javascript, or with a library (there are many).
use $_POST['find']
on your refreshed page.
In this case most people would use AJAX, which uses the Document Object Model (DOM) to update only certain portions of the page. There is a good tutorial if you want an overview here:
http://www.tizag.com/ajaxTutorial/ajaxphp.php
If you do the whole tutorial you'll have enough information to complete this task. It's a lot of information, but really useful- the tutorial is even structured as an order form!
Small tip: you may want to use something like:
$_SESSION['order'][$_POST['id']]++;
instead. That'll make it much easier to see just how much any particular item the user has in their cart. With your method, they could potentially have more than one of an item residing in different indexes of the order array.
精彩评论