hide - Understanding jQuery
hide - Understanding jQuery
hide - Understanding jQuery
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
What’s important is the return value. The example above ignores the return value completely.<br />
Luckily, <strong>jQuery</strong> allows us to capture it. In order for us to see this in action, we need to use a<br />
slightly different syntax shown below:<br />
$.ajax( { url: “script.php”,<br />
type: “post”,<br />
success: function(result) { alert(result); }<br />
});<br />
Still the same function. The only difference is that now we are using JSON with objects inside:<br />
url, type and success. JSON is discussed earlier in this book.<br />
Note that the “result” parameter passed to the success function is the data received from the<br />
script. You are the one to decide what that returned data should be within your “script.php”<br />
file. Again, it can be XML, plain text or any other kind of data, depending on the purpose of<br />
your asynchronous HTTP request. You can format the function above in any way you wish,<br />
but the point is that you specify either the POST or GET type using the “type” parameter.<br />
Remember that everything in Javascript is an object? This is what makes it possible to assign a<br />
function closure to the success parameter in the example above. Even “script.php” and “post”<br />
values are actually objects of type String.<br />
What are some of the things that Ajax can be used for?<br />
Forms: <strong>jQuery</strong> Ajax is incredibly helpful when dealing with HTML web forms. Using selectors<br />
it is possible to collect and send all values from all fields of the form directly to your form<br />
processing script without having to retype the id’s, names or values from the form elements.<br />
Let’s take a look at how you can use <strong>jQuery</strong> selectors along with Ajax to submit information<br />
from an arbitrary form. Let’s say we have a form like this:<br />
<br />
<br />
<br />
<br />
<br />
<br />
Note that there is no need to use the form tag at all unless you think you must. We’re avoiding<br />
the regular HTML form submit and overriding it with the way we want it to work.<br />
38