Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-09-2012, 11:27 PM   PM User | #1
anarchoi
New Coder

 
Join Date: May 2007
Posts: 56
Thanks: 4
Thanked 0 Times in 0 Posts
anarchoi is an unknown quantity at this point
Submit a form with AJAX without reloading

For some reason i need to submit a form to an external website without changing the page. I'm a newbie with AJAX and i don't know how to do it so i would really appreciate if someone could help me.

Here's the form i need to submit:

Quote:
<form action="https://nidieunimaitre.spreadshirt.com/shop/basket/addtobasket" method="post" name="tshirt_form" id="tshirt_form">
<input type="hidden" name="product" id="productId" value="101894422"/>
<input type="hidden" name="article" id="articleId" value="10568008"/>
<input type="hidden" name="view" id="currentView10568008" value="351"/>
<input type="hidden" name="color" id="productColor10568008" value="2"/>

Size
<select id="size" name="size">
<option value="2" >S</option>
<option selected value="3" >M</option>
<option value="4" >L</option>
<option value="5" >XL</option>
<option value="6" >XXL</option>
</select>
<br>

Quantity
<select id="quantity" name="quantity">
<option selected value="1" >1</option>
<option value="2" >2</option>
</select>
</form>


<div id="other">
SUBMIT FORM
</div>


<script>
$('#tshirt_form').submit(function() {
alert('wtf do i need to do now ?');
return false;
});

$('#other').click(function() {
$('#tshirt_form').submit();
});
</script>
Thanks a lot !
anarchoi is offline   Reply With Quote
Old 11-10-2012, 02:53 AM   PM User | #2
optimizer123
New Coder

 
Join Date: Oct 2012
Location: Basement
Posts: 88
Thanks: 9
Thanked 0 Times in 0 Posts
optimizer123 is an unknown quantity at this point
Hey there,

I suggest you post this in the AJAX subforum here:

http://www.codingforums.com/forumdisplay.php?f=55
optimizer123 is offline   Reply With Quote
Old 11-10-2012, 03:28 AM   PM User | #3
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,307
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
you need the .ajax() function http://api.jquery.com/jQuery.ajax/

- Id write an example but if you do a quick forum search youll find alot of examples ( it's about as common a question as how to show and hide elements on click)
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Old 11-10-2012, 03:55 AM   PM User | #4
anarchoi
New Coder

 
Join Date: May 2007
Posts: 56
Thanks: 4
Thanked 0 Times in 0 Posts
anarchoi is an unknown quantity at this point
I'd really appreciate an example of a script that i could use to transmit the data of my form to the external website once the form is submitted because i'm really lost
anarchoi is offline   Reply With Quote
Old 11-10-2012, 10:07 PM   PM User | #5
anarchoi
New Coder

 
Join Date: May 2007
Posts: 56
Thanks: 4
Thanked 0 Times in 0 Posts
anarchoi is an unknown quantity at this point
I have tried following this tutorial:
http://www.simonerodriguez.com/ajax-...ubmit-example/

But it's not working I don't understand what i'm doing wrong

Code:
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <script src="scripts/ajaxsbmt.js" type="text/javascript"></script>
</head>
<body>

<form name="MyForm" action="https://nidieunimaitre.spreadshirt.com/shop/basket/addtobasket" method="post"
onsubmit="xmlhttpPost('https://nidieunimaitre.spreadshirt.com/shop/basket/addtobasket, 'MyForm', 'MyResult', '<img src=\'pleasewait.gif\'>'); return false;">
    <input type="hidden" name="product" id="productId" value="101894422"/>
    <input type="hidden" name="article" id="articleId" value="10568008"/>
    <input type="hidden" name="view" id="currentView10568008" value="351"/>
    <input type="hidden" name="color" id="productColor10568008" value="2"/>

Size
<select class="b-core-ui-select__select" id="size" name="size"><option value="2" >S</option><option selected value="3" >M</option><option value="4" >L</option><option value="5" >XL</option><option value="6" >XXL</option></select>
<br>

Quantity
<select class="b-core-ui-select__select" id="quantity" name="quantity">
<option selected value="1" >1</option>
<option value="2" >2</option>
</select>

<br>

<input name="send_button" type="submit" value="Send" />
</form>


<div id="MyResult"></div>

</body>
</html>
anarchoi is offline   Reply With Quote
Old 11-10-2012, 11:12 PM   PM User | #6
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
You're missing an apostrophe after /addtobasket:

Code:
onsubmit="xmlhttpPost('https://nidieunimaitre.spreadshirt.com/shop/basket/addtobasket', 'MyForm', 'MyResult', '<img src=\'pleasewait.gif\'>'); return false;">
I haven't checked beyond this.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 11-10-2012, 11:19 PM   PM User | #7
anarchoi
New Coder

 
Join Date: May 2007
Posts: 56
Thanks: 4
Thanked 0 Times in 0 Posts
anarchoi is an unknown quantity at this point
Thanks, it was a dumb error.

Ok now the script *seems* to send the data (at least it doesnt reload the page)

But after clicking the submit button, the basket is still empty

When sending the form, a product is supposed to be added to the basket here:
https://nidieunimaitre.spreadshirt.com/shop/basket/

It works when i use a normal form, but doesn't work when i try to submit with AJAX
anarchoi is offline   Reply With Quote
Old 11-10-2012, 11:35 PM   PM User | #8
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
I am unable to assist further, but suspect it might be a restriction of 'cross-domain posting'.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 11-11-2012, 12:09 AM   PM User | #9
anarchoi
New Coder

 
Join Date: May 2007
Posts: 56
Thanks: 4
Thanked 0 Times in 0 Posts
anarchoi is an unknown quantity at this point
damn

Any idea how i could submit a form to another domain without having to reload the page ?

Currently i was using a method to submit the forum into an hidden iframe. It works very good BUT doesn't work in safari browser. I have explained my problem with this method here:
http://www.codingforums.com/showthre...34#post1290234

So, submit to iframe is buggy and AJAX submit doesn't work... Is there another way ?
anarchoi is offline   Reply With Quote
Old 11-11-2012, 12:36 AM   PM User | #10
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Maybe investigate HTML5/XHR2 for modern browsers.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Reply

Bookmarks

Tags
ajax, form, html, javascript, submit

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:13 PM.


Advertisement
Log in to turn off these ads.