Detecting an AJAX request in PHP
After going through this recipe you will be able to distinguish between AJAX requests and simple HTTP requests in your PHP code.
Getting ready
Create a new directory named Recipe3
in the chapter2
directory. Inside it create an HTML file named index.html
and another PHP file check.php
.
How to do it...
Open the
index.html
file and create a button that will load a string from a PHP file using the$.get()
method.<html> <head> <title>Detecting AJAX Requests</title> </head> <body> <form> <p> <input type="button" value="Load Some data"/> </p> </form> </body> </html>
Next, include jQuery and write the code for a
click
event of the button. Clicking on the button will simply send an AJAX request tocheck.php
, which will return a string. The response string will be appended to the page after the input button.<script type="text/javascript" src="../jquery...