php code to manage shopping cart... question here.
Moderator: General Moderators
-
grabber_grabbs
- Forum Commoner
- Posts: 60
- Joined: Mon Oct 10, 2011 6:13 pm
php code to manage shopping cart... question here.
Hi there, and thanks for your help.
I have a shopping cart on my website that is working not too bad. Not too bad until i tried to insert a search fonction to it.
what i dont like is, let say i make a search with the word electrical , the system show only item that has electrical in the description (and this is ok), but if i click on my add to cart button, it does add to the cart, but it does refresh the page and i am back showing all the items (like if there was no search done) so if i want to continue searching in the electrical, i have to type "electrical" again in the search field...
I would like it to work the same way as when you click the back button (back button of the browser) it brings you back where you were, at the same place on the page, with the same search word in the search field....
I have been searching on this without success.
this is a simple expaination of my page.php
>> the following form is located at the top of the page, first form
<form id="form1" name="form1" method="post" action="">
Search Key :<input type="text" name="varsearch" id="varsearch" size="40" value = <?php echo $varsearch; ?> >
</form>
then in the middle of the page, i have a "while" command that read the whole database and make a form for each item
while ($db_field = mysql_fetch_assoc($result)) {
$is_search = stristr($desc, $varsearch);
if($is_search !== FALSE) {
Echo " <P><FORM NAME=order ACTION=\"managecart.html\" onSubmit=\"AddToCart(this);\"> ";
Echo " <input type=\"image\" src=\"./images/acheter.gif\" border=0 value=\"Add to Cart\" align=top> ";
Echo " </FORM> ";
}
}
so basically here is makes a <form> for each item that make the stristr() different than false (this is fine)
It make no difference if ACTION=magementcart.html is in the <form> or not... so when it execute the
addtocart(this) and come back, it refresh the page with all the items in inventory.... this is what i dont like because the customer has to retype in the search field to continue their shopping.
thanks for your help
I have a shopping cart on my website that is working not too bad. Not too bad until i tried to insert a search fonction to it.
what i dont like is, let say i make a search with the word electrical , the system show only item that has electrical in the description (and this is ok), but if i click on my add to cart button, it does add to the cart, but it does refresh the page and i am back showing all the items (like if there was no search done) so if i want to continue searching in the electrical, i have to type "electrical" again in the search field...
I would like it to work the same way as when you click the back button (back button of the browser) it brings you back where you were, at the same place on the page, with the same search word in the search field....
I have been searching on this without success.
this is a simple expaination of my page.php
>> the following form is located at the top of the page, first form
<form id="form1" name="form1" method="post" action="">
Search Key :<input type="text" name="varsearch" id="varsearch" size="40" value = <?php echo $varsearch; ?> >
</form>
then in the middle of the page, i have a "while" command that read the whole database and make a form for each item
while ($db_field = mysql_fetch_assoc($result)) {
$is_search = stristr($desc, $varsearch);
if($is_search !== FALSE) {
Echo " <P><FORM NAME=order ACTION=\"managecart.html\" onSubmit=\"AddToCart(this);\"> ";
Echo " <input type=\"image\" src=\"./images/acheter.gif\" border=0 value=\"Add to Cart\" align=top> ";
Echo " </FORM> ";
}
}
so basically here is makes a <form> for each item that make the stristr() different than false (this is fine)
It make no difference if ACTION=magementcart.html is in the <form> or not... so when it execute the
addtocart(this) and come back, it refresh the page with all the items in inventory.... this is what i dont like because the customer has to retype in the search field to continue their shopping.
thanks for your help
Re: php code to manage shopping cart... question here.
What if you switched your form to GET or stored their search terms in session data and checked for those on page load?
-
grabber_grabbs
- Forum Commoner
- Posts: 60
- Joined: Mon Oct 10, 2011 6:13 pm
Re: php code to manage shopping cart... question here.
thanks for helping here.
as you probably notice, i have the PhP variable $varsearch in the value parameter (see below)
Search Key :<input type="text" name="varsearch" id="varsearch" size="40" value = <?php echo $varsearch; ?> >
is it normal that the $varsearch become "" when the page refresh, if so, how can i save the variable $varsearch and how can i load it upon reloading the php page ?
thanks again
as you probably notice, i have the PhP variable $varsearch in the value parameter (see below)
Search Key :<input type="text" name="varsearch" id="varsearch" size="40" value = <?php echo $varsearch; ?> >
is it normal that the $varsearch become "" when the page refresh, if so, how can i save the variable $varsearch and how can i load it upon reloading the php page ?
thanks again
Re: php code to manage shopping cart... question here.
So you're not leaving the page when you add something to cart? How are you doing it, then? Why is the page being refreshed?
-
grabber_grabbs
- Forum Commoner
- Posts: 60
- Joined: Mon Oct 10, 2011 6:13 pm
Re: php code to manage shopping cart... question here.
Yes it is executing managecart.html that is outside, also the addtocart(this) is an external script.
how would i save the $varsearch value in order to load it back upon reloading the page....
Let say i search electrical, it shows 25 different items, then if i click <view cart> that redirect me to the view cart function. If i click the back button (internet explorer back button) it bring me exactly where i was prior the <view cart> function pressed.. this is how i would like it to work with i click <buy now> button...
the easiest way, as you suggested is to save the session variable, and reload them upon refreshing the page...
is there a function in php that allow to do this ?
how would i save the $varsearch value in order to load it back upon reloading the page....
Let say i search electrical, it shows 25 different items, then if i click <view cart> that redirect me to the view cart function. If i click the back button (internet explorer back button) it bring me exactly where i was prior the <view cart> function pressed.. this is how i would like it to work with i click <buy now> button...
the easiest way, as you suggested is to save the session variable, and reload them upon refreshing the page...
is there a function in php that allow to do this ?
Re: php code to manage shopping cart... question here.
Put session_start() at the top of the page. When the search form is submitted, add $_SESSION['searchvar'] = $searchvar. Finally, when you load the page, check if $_SESSION['searchvar'] exists and is not empty. If so, perform your query before displaying any items.
Alternately, you could have your add-to-cart function be an AJAX call so you're never actually leaving the page. This is probably a little more involved, though.
Alternately, you could have your add-to-cart function be an AJAX call so you're never actually leaving the page. This is probably a little more involved, though.
-
grabber_grabbs
- Forum Commoner
- Posts: 60
- Joined: Mon Oct 10, 2011 6:13 pm
Re: php code to manage shopping cart... question here.
where exactly should i put that $_SESSION['searchvar'] = $searchvar ?
Should i put it just in the form command (before the AddtoCart fonction)
Echo " <P><FORM NAME=order ACTION=\"managecart.html\" onSubmit=\"AddToCart(this);\"> ";
if yes,
i am trying to add it to the command but DW is giving me and error....
Echo " <P><FORM NAME=order onSubmit=<?php $_SESSION['searchvar'] = $searchvar; ?> ; \"AddToCart(this);\"> ";
or maybe is it possible to use the hidden input command, but i just cannot find a way to write it,,, mixing php with html here....
what do you think about using $_SESSION to save the url ? would this prevent the page to reload.
the following command is just what i would need
window.history.back()
but since i dont change url, this command is no use...
thanks.
thanks for your help
Should i put it just in the form command (before the AddtoCart fonction)
Echo " <P><FORM NAME=order ACTION=\"managecart.html\" onSubmit=\"AddToCart(this);\"> ";
if yes,
i am trying to add it to the command but DW is giving me and error....
Echo " <P><FORM NAME=order onSubmit=<?php $_SESSION['searchvar'] = $searchvar; ?> ; \"AddToCart(this);\"> ";
or maybe is it possible to use the hidden input command, but i just cannot find a way to write it,,, mixing php with html here....
what do you think about using $_SESSION to save the url ? would this prevent the page to reload.
the following command is just what i would need
window.history.back()
but since i dont change url, this command is no use...
thanks.
thanks for your help
Re: php code to manage shopping cart... question here.
I didn't realize you were using JavaScript to submit the form. Maybe you want to look at the AJAX solution after all.
-
grabber_grabbs
- Forum Commoner
- Posts: 60
- Joined: Mon Oct 10, 2011 6:13 pm
Re: php code to manage shopping cart... question here.
Celauran, i did find a way to avoid the refresh, i simply added the return false to the onsubmit=
Echo " <P><FORM NAME=order onSubmit=\"AddToCart(this); return false \"> ";
thanks for your help
Echo " <P><FORM NAME=order onSubmit=\"AddToCart(this); return false \"> ";
thanks for your help