views:

212

answers:

1

Hello! I'm using jQuery Form (http://jquery.malsup.com/form/) to get send data to a form - is there a way I can then, without refresh, put the results page that's generated by the form into a div on the page?

Any advice appreciated!

+2  A: 

I would suggest not using that form plugin - it was made back in the days before there was an easy way to serialize form data using jQuery, and serves no real purpose any more. I would suggest something like this:

$("form").submit(function() {
    $.post($(this).attr("action"), $(this).serialize(), function(html) {
        $("#someDiv").html(html);
    });
    return false; // prevent normal submit
});

If you insist on using it, you can set the target option to a selector of the element(s) you would like to fill up:

// prepare Options Object 
var options = { 
    target:     '#divToUpdate', 
    url:        'comment.php', 
    success:    function() { 
        alert('Thanks for your comment!'); 
    } 
}; 

Take a look at http://jquery.malsup.com/form/#options-object for more info.

To prevent refresh, just make sure the form's onsubmit event returns false:

<form method="post" onsubmit="return false">
karim79
Thanks for this, I'm going with your recommendation of not using the plugin. However, forgive me if I'm being thick but when I'm submitting the results div is being filled up with the form page again!Any ideas?
Cordial
@Cordial - You're not being thick :) You will have to tell the server to send back only the portion of the HTML that you want to use to populate the DIV (which is the more efficient way), or pick out the specific html from the server response that you want, e.g. `$("#someDiv").html($(html).find('#results'));`
karim79
Excellent - many, many thanks!
Cordial