I've got this simple script where I want to post data using AJAX to the server and then spit it out (echo), but it won't work...
This is my output:
Your viewport width is 1338x684
Ajax wasn't initialized!
And in this case, the Width and Height are only set to 880px * 495px when the PHP script doesn't get any data.
The web page head of testajax.php:
<head>
<script type='text/javascript' src='js/jquery-1.7.2.min.js'></script> <!-- Main Java library script-->
<!-- The following 3 scripts are utilized by the Accordion Menu in the aside side bar -->
<script type='text/javascript' src='js/jquery.cookie.js'></script> <!-- Acco. Menu script-->
<script type='text/javascript' src='js/jquery.hoverIntent.minified.js'></script><!-- Acco. Menu script-->
<script type='text/javascript' src='js/jquery.dcjqaccordion.2.7.min.js'></script> <!-- Acco. Menu script-->
<!--The following 2 scripts are utilized by the Pan and Zoom Slide Show -->
<script type="text/javascript" src="js/jquery.carouFredSel-6.2.1.js"></script>
<script type="text/javascript" src="js/panzoomslideshow.js"></script>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Testing Ajax</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/normalize.min.css"> <!-- used to normalize-->
<link rel="stylesheet" href="css/main.css"> <!-- Main CSS styling-->
<script type="text/javascript">
document.write('<p>Your viewport width is '+ window.innerWidth+'x'+window.innerHeight+'</p>');
$(document).ready(function() {
$.ajax({
url: "ajaxresponse.php",
type: "POST",
dataType: "json",
data: {
width : window.innerWidth,
height : window.innerHeight
},
success: function( data, textStatus, jQxhr ){
$("#response pre").html( JSON.stringify( data ) );
},
error: function( jqXhr, textStatus, errorThrown ){
console.log( errorThrown );
}
});
});
</script>
<!-- ********** panzooom.php prints the java script to make a pan and zoom transition slide show, depending on the width and heigth of the viewport *********** -->
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<script>window.html5 || document.write('<script src="js/vendor/html5shiv.js"><\/script>')</script>
<!--<![endif]-->
<?php include 'ajaxresponse.php';?>
</head>
And here is the ajaxresponse.php file:
<?php
if (is_ajax()) {
if (isset($_POST["data"]) && !empty($_POST["data"])) { //Checks if data value exists
$data = $_POST["data"];
echo "Ajax was initialized :)<br>";
echo '<p>var_dump($_POST):<br>';
var_dump( $_POST );
}else{
"Ajax was initialized, but the value isn't set or it's empty (same same)!<br>";
}
}else{
echo "Ajax wasn't initialized!<br>";
}
//Function to check if the request is an AJAX request
function is_ajax() {
echo "\$_SERVER['HTTP_X_REQUESTED_WITH'] -> " . $_SERVER['HTTP_X_REQUESTED_WITH'] . "<br>";
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}
?>