Rose Perrone
Rose Perrone

Reputation: 63616

Intercepting default behavior for form submission

When the following code executes, the parameters appear in the url, and no inner HTML within the table changes. The documentation says the default request method is a POST when data is provided.

$('form').submit(function() {
    var data = $(this).serializeArray();
    $('#items-table').load("drawer.php", data)
});

The data is outputted correctly as MM=01&dd=01&yyyy=2001&HH=12&mm=00&ss=00&entername=Dr.+Clock&optionsRadios=option3&enteritemid=, but it appears as parameters to the current page rather than the page identified in the load() function, drawer.php.

EDIT: Even when I use serializeArray() to make sure an object is passed as the second parameter to load() rather than a string, I still find that a GET request is called, and the current page's URL gains these parameters. The response from "drawer.php" does not appear in the div.

SECOND EDIT: It turned out I had failed to intercept the form's action, because I didn't include this line: return false; at the end of the submit function.

Upvotes: 0

Views: 128

Answers (3)

Rose Perrone
Rose Perrone

Reputation: 63616

I had failed to intercept the form's action, because I didn't include this line: return false; at the end of the submit function. Including this line solved the problem.

Upvotes: 1

The Alpha
The Alpha

Reputation: 146269

The POST method is used if data is provided as an object; otherwise, GET is assumed.

$('#items-table').load("drawer.php", {form_data:data})

So pass an object/{} instead of string because serialize() method creates a text string delimated with & and also you've used $data instead of data.

Upvotes: 0

Torsten Walter
Torsten Walter

Reputation: 5802

You are using var data = $(this).serialize();

The doc says:

The .serialize() method creates a text string in standard URL-encoded notation.

http://api.jquery.com/serialize/

You need to provide an object as the second parameter of load to use POST.

Upvotes: 0

Related Questions