$(document).ready (function()
{ 
	$('.LiveStatsBtn').click(function(){		
		var filename=$(this).siblings().children().val();
		var jqxhr = $.ajax({			
			type: "POST",
           url: "getfirst.php" ,
           data: {
           			'foo': 'bar'

          		 }

           }) 
		    .done(function(msg) { alert(msg); })
		    .fail(function() { alert("error"); })
		    .always(function() { alert("complete"); });
		
	});
});

getfirst.php:

<?php

print_r($_POST);

?>

This returns a prompt:

Array
(
    [foo] => bar
)

Ver 2:

<?php

if (isset($_POST['filename'])) {
	print "I got a filename".$_POST['filename'];
} else {
	print "Failed miserably";
}
?>

Script:

$(document).ready (function()
{ 
	$('.LiveStatsBtn').click(function(){		
		var filename=$(this).siblings().children().val();
		var jqxhr = $.ajax({			
			type: "POST",
           url: "getfirst.php" ,
           data: {
           			'filename': filename
          		 }

           }) 
		    .done(function(msg) { alert(msg); })
		    .fail(function() { alert("error"); })
		    .always(function() { alert("complete"); });
		
	});
});

Output: I got a filename/var/www/pinginfo/versatileit
Here, the string “/var/www/pinginfo/versatileit” was passed from the displayed output to server, and got back the server response.


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.