Help with Ajax .... need some clarification
Can any one please explain this:
var js_var = 'hello';
$.ajax({
type: "POST",
url: "some.php",
data: "js_var="+js_var,
success: function(msg){
alert( "Data Saved: " + msg );
}
});
I need to know what is returned by the PHP file 开发者_StackOverflow社区in this case... The PHP file can be assumed to return whether jsvar exists in the db or not...
The PHP file will return whatever the result of processing the data params (js_var=hello
) is. The output will be received by the client as a string (msg
).
Your output from PHP script either with echo/print
or even HTML is returned in the msg
argument of success
callback function.
For example, if you do this from PHP script:
echo 'hello world';
The msg
will be equal to hello world
Assuming that this is in reference to PHP Javascript variable help then some.php
can do whatever you need to do. The actual some.php
was just a place holder for whatever script you needed run was named.
Instead of the output of that PHP file being sent and rendered in browser, it is sent and put into the msg
variable. What you send back could be a block of HTML to be shoved into a div
for rendering, a simple OK, or a JSON object, or XML. As you are writing the code on both ends of the communication, you can do whatever you want.
For example, if you were creating a user creation form, you could have a script, that after the user name box loses focus, goes and makes an AJAX call, then returns either OK
or an error message, then you could display that next to the user name box.
EDIT:
As other people have mentioned, it would be best going through a few tutorials about jQuery and AJAX in general. A quick google turns up a few decent ones:
http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery http://www.ibm.com/developerworks/library/x-ajaxjquery.html http://www.ibm.com/developerworks/opensource/library/os-php-jquery-ajax/index.html
I've found that the IBM developerworks articles to be rather clear and concise time and again.
use firefox , firbug or ie fiddle to the see the output.
Firebug is really nice , there is a panel called net , open that and you can see the result from php.
精彩评论