PHP如何将数据发送到Ajax?

I've been studying PHP for a while now, and Ajax for about a month or two. I still can't figure out how does PHP send data to Ajax and how does Ajax retrieve the data.

Can anybody post an example or something?

Maybe After submitting some text.. PHP sends the data to the Database, but how would Ajax also receive it and post it to a specified field without refreshing?

If it helps, I use jQuery a lot, so i'd be using it with AJax too,

If you make an Ajax request to a PHP script,

  • Anything you echo in PHP will turn up in the Ajax request's response body. As @webbiedave says, that can be for example clear text, or XML, or HTML, or JSON encoded data.

  • Any status code you throw in the PHP script (e.g. header("HTTP/1.1 404 Not Found")) will be reflected in the Ajax result. If you throw a non-successful HTTP status code (e.g. 404 or 500) the Ajax request will fail, and (if you use jQuery's Ajax) the error callback will be called.

It just sends a request to the server like a browser, only in the background, without reloading. The received data is the content of the page. The same you would see if you'd open the url in the browser.

It's actually a lot less complicated than you may think. Think of AJAX as just requesting any old webpage, the trick being that it does it behind the scenes, "on the fly," without regard to anything in the URL bar of the browser.

In that sense, you're almost asking the wrong question (almost) because it doesn't matter (sort of) how PHP serves the page. AJAX can request any page that's served up from PHP. So, for example, say you have a simple PHP page that looks like this:

<?php
$content = '<div class="content">content</div>';
echo $content;
?>

And you save the file as /content.html

From another page, you can load that content dynamically, using AJAX, and as jQuery is your preference, that might look like this:

$('#result').load('/content.html #content');

.load() is specific kind of AJAX function (there are several, the most basic being .ajax() I believe) that loads HTML content directly into a given page. In this case, what's happening above is that when you call the .load() function, it loads via AJAX /content.html into memory, looks for the element called #content (which happens to be a div), then inserts that loaded #content element into the #result element.

Note, that the reason I'm saying "almost" and "sort of" above is that it will matter, depending on what you're doing, how you format your PHP page. For example, it's common to request pages with AJAX that are formatted in either XML, JSON, or just plain old HTML snippets. That is, of course, a much more dense subject, but here are a couple example tutorials:

http://www.xoops.org/modules/news/article.php?storyid=5103 http://www.factsandpeople.com/facts-mainmenu-5/26-html-and-javascript/89-jquery-ajax-json-and-php http://www.phpriot.com/articles/php-json-jquery-ajax-screencast

Hope that at least helps. There are A THOUSAND tutorials online, however, and simply searching on Google should yield plenty of results.