473,407 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

How can I stop page jumping to top after form submission

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
2 1954
Claus Mygind
571 512MB
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
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

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

Similar topics

5
by: ratlhead | last post by:
Hey all, I'm attempting to do some form processing on a server that has register_globals off, however, I've run into a confusing situation and need some help. Basically, the form is a...
1
by: Newbie | last post by:
OK, this may be impossible since I'm using 3rd party shopping cart ASP software, but I've been able to finagle a lot of other stuff I thought wouldn't work, so here we go: I'm using a form in...
6
by: Charles Banas | last post by:
weird subject - i hope more than just one curious regular will hear me out. :) ok, i've got a bit of a big problem, and i need answers as soon as possible. i know this forum is meant for web...
2
by: adawson | last post by:
Hi, I need to execute some code when form submission is stopped before the page is returned from the server. For example, a user clicks a button to submit the form, realizes they've made a...
3
by: Chris Smith | last post by:
Good morning, Is there a good way to use JavaScript to send a form submission, but get back the response as a string, rather than loading it into a page? I could write the code to send the...
6
by: brettev | last post by:
World, I work at a university where the professors have a system to input grades for assignments and calculate final grades, which is output to an excel file. they are then required to get on a...
6
by: Oleg Konovalov | last post by:
Hi, I have a Java/JavaScript GUI application where I perform a lot of long DB operations , which takes 5-60 secs to perform. Sometimes user double-clicks the button or just gets impatient and...
3
by: Niall | last post by:
The short version: how does one get Javascript to abort a form submission/page load half way through? Long version: I have a page, the guts of which can be summarised as: <script>
6
by: Stanimir Stamenkov | last post by:
I have a form without a submit button like: <form name="form1" action="" onsubmit="alert('submit ' + this.name);"> <div> <label>Field 1: <input type="text" name="field1"...
0
Thekid
by: Thekid | last post by:
I'm trying to auto send a form submission to a website but it isn't working. I've done this before and it worked but for some reason, using the same basic code, it doesn't seem to submit it. I'm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.