1

When a $.post is made to a php page, or any other type of script, how is the php file executed on the server side?

For instance, if I have a php page that looks for a certain parameter to be passed in, and if that requirement is made, it calls echo to write something to the page. However, when I make that post, the actual php page is never loaded in my browser, so what exactly executes?

I feel like I'm asking something along the lines of..."If a tree falls and no one is around, does it still make a noise?" :)

user2592690
  • 265
  • 2
  • 11
  • The PHP script is executed just as if a person posted to the page normally. – Dave Chen Jul 21 '13 at 21:27
  • The echoed output is returned as the ajax response.. – Fabrício Matté Jul 21 '13 at 21:27
  • 1
    In a general sense the PHP doesn't care where the request came from, it just knows that a request came in, perhaps with certain parameters, and it sends a response back. If the request was a "normal" browser request for a page, like when you type an address in the address bar or click a normal anchor tag then the browser loads the response as a new page. If the request was Ajax then the browser gives the response to the JavaScript involved. Or the request may not have even come from a browser at all, but PHP is happy to send a response... – nnnnnn Jul 21 '13 at 21:30
  • Did you ever use curl or wget? It's the same. You make a request to the server, receive the response and process it. In case of Ajax, the response is processed by a JavaScript function. I think if you learn some network basics this will become clearer (but it's not in the scope of Stack Overflow). – Felix Kling Jul 21 '13 at 21:32
  • 1
    No difference from the servers point of view, **except** for the most part `$_SERVER['HTTP_X_REQUESTED_WITH']` will be filled with `xmlhttprequest` if its a **jQuery** AJAX request – Lawrence Cherone Jul 21 '13 at 21:36
  • Thanks to all of you. – user2592690 Jul 21 '13 at 21:38
  • No, I've never used curl or wget. I am just learning programming on the web side of things. What brought the question up, was in a tutorial I was reading about passing params to a php page, the author has the php page echo something on a certain parameter. So I started wondering what happened to that echo. – user2592690 Jul 21 '13 at 21:40

3 Answers3

2

The server-side execution is no different in an AJAX call vs. any other call. For some clarification, take a look at the Network tab in a browser debugging tool (such as FireBug or Chrome tools). Watch the requests made to the server when the page loads, as well as when AJAX requests are made. They all share the same structure.

An HTTP request is sent to the server, which consists mainly of headers and perhaps content, and the server responds with an HTTP response, which consists mainly of headers and content. Requests also have verbs (GET, POST, etc.) and responses also have codes (200, 404, 500, etc.). These details are all the same regardless of whether it's an AJAX request or not.

For example, if you were to make an AJAX request to a "normal" PHP page, you'd see in the browser debugging tool that the response contains all the HTML for that page. The server saw no difference, it just responded to the request.

It's up to the client (the web browser) to know what to do with the response. For a "normal" page load the browser renders the HTML page. For an AJAX request the JavaScript would need to handle the response.

David
  • 208,112
  • 36
  • 198
  • 279
  • 1
    This is a good answer. but to reiterate a couple things from other comments: PHP will know when it's an AJAX request via `$_SERVER['HTTP_X_REQUESTED_WITH']` = `xmlhttprequest`, and you can't interrupt the script and print_r/vardump some data to test it. A response ALWAYS comes back to the ajax script whether you're displaying that response or not. This makes it slightly harder to debug your PHP – AgmLauncher Jul 21 '13 at 21:43
  • Right now, I'm learning through trial and error, as I'm sure most people do...a lot of great information to digest. Helps a lot. Thanks for your reply. – user2592690 Jul 21 '13 at 21:48
0

As far as the server (and PHP) is concerned, a browser did load the page. It's only on client side that you don't see anything, but the "page" is still returned as the AJAX result.

Amber
  • 507,862
  • 82
  • 626
  • 550
0

To demonstrate:

If your $.post call looks like this:

$.post('test.php',
       { param1 : "value1" },
       function(data) {
         console.log(data)
       }
);

and your test.php script looked like this

<?php

echo $_POST["param1"]

?>

then console.log(data) will output "value1".

What your php script echos is available in the first parameter to the success handler of the $.post call. It does not get written to your html page directly.

go-oleg
  • 19,272
  • 3
  • 43
  • 44