473,472 Members | 2,264 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How can I stop page jumping to top after form submission

2 New Member
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 1956
Claus Mygind
571 Contributor
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
razar63
2 New Member
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: 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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.