¿Cómo envía PHP datos a 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,

preguntado el 09 de enero de 11 a las 00:01

HTTP is a call and response protocol. When the client uses AJAX to send a request to the server, have your PHP output XML, JSON or HTML. -

When you make a call to a PHP page with Ajax, anything that is on the page is passed back to your Javascript via XMLHttpRequest.responseText. Read an Ajax tutorial and it will explain everything to you. -

I understand that, I still don't get it. -

Here's a link to a simple example of this: w3schools.com/PHP/php_ajax_php.asp -

What exactly don't you get? How the protocol works? The concept behind Ajax? How to use the programming interface? -

3 Respuestas

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 casi 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:

$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.

Respondido el 09 de enero de 11 a las 03:01

Thank you for a very detailed answer. I understand much better now - nowayyy

If you make an Ajax request to a PHP script,

  • Cualquier cosa que 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.

Respondido el 09 de enero de 11 a las 03:01

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.

Respondido el 09 de enero de 11 a las 03:01

Yes. This. Ajax is just another HTTP request. - Matt Ball

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.