By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,982 Members | 1,919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,982 IT Pros & Developers. It's quick & easy.

How can I stop page jumping to top after form submission

P: 2
Hi I have modified a simple shopping cart tutorial which works fine however after the item is added to the cart it jumps to the top of the page. I have tried using the preventDefault command but it dosn't work.

Expand|Select|Wrap|Line Numbers
  1. <script type="javascript">
  2.     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
  3. <script language="javascript">
  4. function addtocart(pid){
  5.     document.form1.productid.value=pid;
  6.     document.form1.command.value='add'; 
  7.     document.form1.submit((function(e){  
  8.        // Cancel the default action
  9.        e.preventDefault();  
  10. })) ; 
  11.     confirm('You have just added another item to the cart ') ;
  12.  
  13. }
  14. </script>
The add to cart function code looks like this
Expand|Select|Wrap|Line Numbers
  1. function addtocart($pid,$q){
  2.         if($pid<1 or $q<1) return;
  3.  
  4.         if(is_array($_SESSION['cart'])){
  5.             if(product_exists($pid)) return;
  6.             $max=count($_SESSION['cart']);
  7.             $_SESSION['cart'][$max]['productid']=$pid;
  8.             $_SESSION['cart'][$max]['qty']=$q;
  9.         }
  10.         else{
  11.             $_SESSION['cart']=array();
  12.             $_SESSION['cart'][0]['productid']=$pid;
  13.             $_SESSION['cart'][0]['qty']=$q;
  14.         }
  15.     }
  16.     function product_exists($pid){
  17.         $pid=intval($pid);
  18.         $max=count($_SESSION['cart']);
  19.         $flag=0;
  20.         for($i=0;$i<$max;$i++){
  21.             if($pid==$_SESSION['cart'][$i]['productid']){
  22.                 $flag=1;
  23.                 break;
  24.             }
  25.         }
  26.         return $flag;
  27.     }
What can I do to prevent the jump to the top of the page after adding an item to the cart?
Nov 17 '14 #1
Share this Question
Share on Google+
2 Replies


Claus Mygind
100+
P: 571
Had the same problem once. Was fully explained after I posted the question in this forum.

Most likely you are executing from an anchor on the page. So you need to remove the # or href="" from the anchor. This in turn will mean you need to create css style so you link looks like a link

Expand|Select|Wrap|Line Numbers
  1.  
  2. <a class="simAnchor" id="'+thisId+'" onclick="useThisId(this, '+c.cFieldName+');">'+thisId+'</a>
  3.  
Nov 24 '14 #2

P: 2
Thanks Claus
I did something similar by redirecting back to a dynamic anchor point on the page. Much easier than trying to get the same result battling with javascript.
Nov 24 '14 #3

Post your reply

Sign in to post your reply or Sign up for a free account.