Results 1 to 5 of 5

Thread: Submit form using javascript on a link

  1. #1
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Submit form using javascript on a link

    I've got a form that is submitted by a link rather than a submit button.

    The following code works great with internet explorer but not firefox, any ideas?

    HTML Code:
    <a href="#" onclick="return submititems(this.form);"><img src="content/images/submit.jpg" /></a>
    	
    <script type="text/javascript" language="javascript">		
    function submititems(thisform) 
    		{ 
    		for (var q=0, e=this.orderqty.elements, i = e.length-1; i > -1; --i)
    			{
    			if (e[i].name && e[i].name=='qty[]' && !isNaN(+e[i].value))
    				{
    				q+= +e[i].value;
    				}
    			}
    		if (q==0)
    			{
    			alert("you have not selected any products");
    			}
    		else
    			{
    			if (q==1)
    				{
    				if (confirm("add " +q+ " product to the shopping cart?")) 
    					{ 
    					document.orderqty.submit();
    					}
    				else
    					{ 
    					return false; 
    					}
    				}
    			else
    				{
    				if (confirm("add " +q+ " products to the shopping cart?")) 
    					{ 
    					document.orderqty.submit();
    					}
    				else
    					{ 
    					return false; 
    					}
    				}
    			}			 
    		} 
    	</script>

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Code:
    onclick="return submititems(this.form);">
    is not valid. It should be something like document.forms[0] or document.getElementByIt('formID') or something to that effect.

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    how exactly do I change my code?
    Do i literally change this.form for what you've suggested?

    Sorry i'm fairly new to javascript.

  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Sorry, I should have been more specific in my last post. Yes, you change the part that I highlighted (this.form) to point to the actual form such as what I posted above.

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  5. #5
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    I've now changed it to:
    HTML Code:
    <a href="#" onclick="return submititems(document.getElementById('product_add'));">
    and changed the id of the form to 'product_add'

    Which again works fine with Internet Explorer but not firefox.

    Also I presume you meant - document.getElementById?

    Any other ideas?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •