View Single Post
Old 11-26-2012, 03:33 PM   PM User | #4
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,306
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
Code:
<form id="form1" method="POST">
<input id="l" name="l" size="45" type="text" value="book">
<input id="submit" name="submit" value="save" type="submit">
</form>


<script>
$('#form1').submit(function(event){

event.preventDefault();

var formdata= $(this).serialize();//$(this) refers to the element you attached the submit function to  .serialize() prepares the values from the form for url submission and does most of the work for you
var carturl = '/content/pages/cart.php';//get your basic cart url

$.ajax({

type: "POST",

url : carturl,//get basic processing page url from variable

data: formdata,

success: function(msg){//msg is the html response from the page you just posted to

$("#contentmainpane").html(msg);//this changes the html to show the response

}
});
});
</script>
hope this makes sense.

ok also, you need to bear in mind some peopel actually do disable javascript. you should make your form work without ajax first. this includes giving your form an action attribute. I liek to do it that way then in your .ajax call you can do this

Code:
var $posttype = $(this).attr('method')
var $carturl = $(this).attr('action')
__________________
- 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

Last edited by DanInMa; 11-26-2012 at 03:37 PM..
DanInMa is offline   Reply With Quote