Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you are fundamentally confused about the page lifecycle of PHP versus JavaScript. PHP is a server-side scripting language. When you request a page from your server (by navigating to it, or initiating an AJAX call), the PHP script runs from start to finish and your server returns its output (which in this case is an HTML page). That's it. PHP is done when the page source is delivered to your browser.</p> <p>JavaScript, on the other hand, is a client-side scripting language. JavaScript operates inside your browser and can make changes to a page once the page is loaded (most notably by altering the DOM).</p> <p>AJAX provides a somewhat tenuous link between the two worlds. Javascript functions like the .post() in jQuery send an asynchronous request back to the server and tell the server to retrieve a certain file. If that file is PHP, the server typically executes it.</p> <p>So in your example here's what happens:</p> <ol> <li>You type the URL for thefilename.php into your browser and hit enter</li> <li>Browser sends a request to the server for thefilename.php</li> <li>Server recognizes thefilename.php as a PHP file and tells PHP to execute the script</li> <li>The PHP script runs. Its output is determined by the data contained in the request body ($_POST in PHP). It outputs some HTML code.</li> <li>The server responds to your request for thefilename.php with the HTML code produced by executing it.</li> <li>Your browser renders the HTML code for you.</li> <li>You click the button</li> <li>JavaScript in your browser sends a request to thefilename.php on your server</li> <li>Your server recognizes thefilename.php as a PHP file and tells PHP to execute the script</li> <li>The PHP script runs. Its output is determined by the posted data. It produces some HTML code.</li> <li>The server responds to your request for thefilename.php with the HTML code produced by executing it.</li> <li>The .post function receives the HTML response and then renders it inside an alert.</li> </ol> <p>I hope that helps explain what's going on. Most likely you want a separate PHP file for your asynchronous call.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload