0

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;  
}
kmario23
  • 57,311
  • 13
  • 161
  • 150
  • 6
    try http://stackoverflow.com/questions/2579254/php-does-serverhttp-x-requested-with-exist-or-not – Rakesh Sharma May 30 '14 at 10:18
  • I was about to link that same answer too, @Rakesh Sharma The HTTP_X_REQUESTED_WITH should not be trusted as a foolproof way of knowing the request comes from AJAX. – GarethL May 30 '14 at 10:20
  • You can find something here http://stackoverflow.com/questions/1885847/jquery-no-x-requested-with-xmlhttprequest-in-ajax-request-header – Manwal May 30 '14 at 10:21
  • 1
    @GarethL: anything particularly wrong with relying on it? – zerkms May 30 '14 at 10:32
  • 1
    I can easily send a `HTTP_X_REQUESTED_WITH` through [.setRequestHeader](http://msdn.microsoft.com/en-us/library/ie/ms536752%28v=vs.85%29.aspx) –  May 30 '14 at 10:36
  • @zerkms The reasons are explained in the link Rakesh Sharma posted, in the most upvoted answer, and the comments beneath it. – GarethL May 30 '14 at 10:42
  • @GarethL: they only mention theoretic possibility of that. Any examples of modern software (firewalls, proxies, etc) that break it? It's 2014 outside here, does this problem still exists or people just blindly spread ancient fears? – zerkms May 30 '14 at 10:43
  • I honestly haven't come across any so far during my own work. I still avoid using it though. I may be paranoid :) – GarethL May 30 '14 at 10:45
  • @GarethL: the thing is that for the last 6 years that what people around speak about. But I haven't ever seen any traces of that mysterious broken proxy server :-) – zerkms May 30 '14 at 10:46

0 Answers0