Forum Moderators: open

Message Too Old, No Replies

jquery ajax form post

         

homeless

5:57 pm on Sep 17, 2010 (gmt 0)

10+ Year Member



Hi,

Most businesses in my area seem to prefer jQuery. As a matter of fact, when I look at job boards I never see Prototype. I am pretty much a beginner with javascript but I was able to use Prototype/Scriptaculous to do what I needed and learn a little javascript along the way. One thing that I absolutely need is a form post with a return.

I've decided to switch over to jQuery, since that seems to be where the industry is headed.

I've been able to get $.post to work. Pretty simple but I need Ajax and I can't figure out how to capture the return.

My Ajax post is below. I've been searching for days on how to capture the return data but I am clueless. Can someone tell me what is missing from the code below...basically how I set the function variable.

I want to replace the the existing id='bottom' with the results of my post.



function ajaxFormSubmit(process, method, url, target)
{
var serial_data = 'xhr=' + process + '&' + $("form").serialize();
$.ajax({
type: method,
url: url,
data: serial_data,
success: valid_signin(serial_data)
});
}

function valid_signin(data, seial_data)
{
alert("made it (serial_data): " + serial_data);
}

Fotiman

6:14 pm on Sep 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The best place to look is the API documents for jQuery.
[api.jquery.com...]

The problem in your example is that you're calling the valid_signin function and assigning the return value of that to the success property.

success: valid_signin(serial_data)

should be

success: valid_signin

Try this:


function ajaxFormSubmit(process, method, url, target) {
var serial_data = 'xhr=' + process + '&' + $("form").serialize();
$.ajax({
type: method,
url: url,
data: serial_data,
success: valid_signin
});
}
function valid_signin(data, textStatus, XMLHttpRequest) {
alert("made it (data): " + data);
}

Note, in the callback method, data is the data that's returned from the server, not the serial_data that you defined earlier.

Hope that helps.

homeless

7:17 pm on Sep 17, 2010 (gmt 0)

10+ Year Member



Thank you :)