1

If all I want to do is submit a form to a PHP processor with POST data and get the result, is there any reason to use the ajax method over the jQuery load method?

Using Ajax requires a bunch of code versus something very simple on one line in jQuery like this:

$("#myDIV").load("myProcessor.php", {field1: target.value});

Hmmm, maybe this only works well with one field? Sorry, just getting back into JS.

Fonewiz
  • 2,065
  • 3
  • 17
  • 17

3 Answers3

0
  • The .load method is is roughly equivalent to $.get(url, data, success) (src).
  • The .get method is a shorthand Ajax function (src).

For a simple ajax call, .load is appropriate. Use .ajax if you want to deal with specials parameters.

PS: If you don't want a fat framework like jQuery, look here : http://microjs.com/#ajax

Fabien Sa
  • 9,135
  • 4
  • 37
  • 44
0

From jQuery website about load

This method is the simplest way to fetch data from the server. It is roughly equivalent to $.get(url, data, success) except that it is a method rather than global function and it has an implicit callback function. When a successful response is detected (i.e. when textStatus is "success" or "notmodified"), .load() sets the HTML contents of the matched element to the returned data.

you can read difference between load ajax on StackOverflow.

But, load is not for submitting a form using post method. Both POST and GET are different methods and works differently, data submitted using GET method will be available in the $_GET array but not in the $_POST.

Community
  • 1
  • 1
The Alpha
  • 143,660
  • 29
  • 287
  • 307
0

$(element).load(...) will automatically insert the server response into the selected element as html (jQuery documentation for .load()). This might result in your server exposing information about the request directly to the user, which is probably not what you're looking to do.

$.ajax() also allows very fine control over what happens during the request. You can accurately intercept exceptions and produce a callback when the request finishes succesfully. These are all reasons to use $.ajax() over $(element).load(...). However, if your only concern is sending the data in the POST header and working with the response, then using $.post with a callback function will probably be the simplest approach. http://api.jquery.com/jQuery.post/

Stephan Heijl
  • 464
  • 6
  • 19