HTML and CSS Reference
In-Depth Information
DETECT AND RESPONDING TO THE AJAX REQUEST
All that's left to do is update the post_comment.php file to detect the AJAX call and return
information about the newly created comment.
Detecting the AJAX request is done by checking for the X-Requested-With header:
$ajax = ( $_SERVER [ 'HTTP_X_REQUESTED_WITH' ] === 'XMLHttpRequest' );
Once we know the request is an AJAX request we can update the code to respond with an
appropriate status code and the data representing the comment. We also need to ensure that
the original functionality is maintained. The post_comment.php code now looks as follows:
<?php
require ( 'Persistence.php' );
$ajax = ( $_SERVER [ 'HTTP_X_REQUESTED_WITH' ] === 'XMLHttpRequest' );
$db = new Persistence ();
$added = $db -& gt ; add_comment ( $_POST );
if ( $ajax ) {
sendAjaxResponse ( $added );
}
Search WWH ::




Custom Search