I'm using the following JS for selecting some text and POSTing using Jquery. I'm able to successfully select and post it but the $_SERVER['HTTP_X_REQUESTED_WITH']
is not set when I 'mouseup'(releasing the mouse press after text selection). Where am I going wrong? please help.
<script type="text/javascript" name="selection" charset="utf-8">
$(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 : 'check.php',
data: 'selection=' + encodeURI(selection),
success: function(data){
//alert('sending the data succeeded in ajax post');
},
error: function(xhr, data, error){
alert('sending the data failed during ajax post');
},
});
}
});
});
</script>
The PHP code is
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{
//mysql_query to INSERT INTO text_selections;
}
I've even tried this, still no luck
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
////mysql_query to INSERT INTO text_selections;
}