I'm trying to post a JS variable to a PHP script but I'm not able to do it. Here are the scripts,
The following JS is embedded in a script named "check.php",
<script type="text/javascript">
$(document).ready(function() {
$('#content-area').mouseup(function() {
var selection = getSelected();
if(selection && (selection = new String(selection).replace(/^\s+|\s+$/g,''))) {
alert('Sending the following text to the server via AJAX: ' + selection);
$.ajax({
type: 'post',
url : 'calculate.php',
data: 'selection=' + encodeURI(selection)
});
}
});
});
</script>
I'm posting the "selection" variable using ajax to The PHP script(calculate.php) where I'm receiving the value as follows.
<?php
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' &&
$selection = trim($_POST['selection'])) {
$selText = $selection;
}
?>
I'm getting a null value at the server-side i.e "calculate.php". Please tell me what is going wrong here?