0
    function hashValue(ID){
                jQuery.ajax({
                    url: "<?php echo get_template_directory_uri(); ?>/getHashvalue.php",
                    data: {ID:ID},
                    success: function(res) {
                        alert(res);
                    }
                });
            }

The above jQuery Ajax call is passing ID as the Parameter successfully.

But the PHP code is not able to receive the ID sent by Ajax.

<?php
if(isset($_POST['ID']))
{
    $hashid = $_POST['ID'];
}
$hash = hash('sha1', $hashid);
print_r($hash);
exit;
?>

Error is as follows

( ! ) Notice: Undefined variable: hashid in G:\wamp\www\wp-content\themes\theme1\getHashvalue.php on line 6
harishannam
  • 2,747
  • 3
  • 30
  • 39

1 Answers1

3

The default HTTP method of jQuery.ajax is a GET request, not a POST request.

http://api.jquery.com/jQuery.ajax/

So you need to be checking $_GET['ID'], not $_POST.

sevenseacat
  • 24,699
  • 6
  • 63
  • 88
  • This solved my problem. Will accept it as answer soon. POST is secured than GET, is it possible to change the Default GET to POST request? – harishannam Mar 15 '14 at 15:24
  • 2
    Yup. Or change the client side to add `type: "POST"` to the ajax() parameters (or use jQuery's [post() shorthand](https://api.jquery.com/jQuery.post/).) – Matt Gibson Mar 15 '14 at 15:24
  • Yes. In your `jQuery.ajax` call, add the option `type: 'POST'`, as described in the docs I linked. – sevenseacat Mar 15 '14 at 15:24
  • @harishannam Could you expand on "POST is secured than GET"? I'd not say either method was more secure in an Ajax call, if that's what you're meaning... – Matt Gibson Mar 15 '14 at 15:25
  • A POST request doesn't send the data as a querystring, but it's still pretty accessible with something like Fiddler etc. – adeneo Mar 15 '14 at 15:26
  • @MattGibson I read in w3schools stating that GET method is not much secure as it stores data in browser. So I thought same applies for Ajax calls. – harishannam Mar 15 '14 at 15:27
  • 1
    Depends on what you mean by "secure". Anything that's in the middle could easily sniff either of them unless they're secured by using an https connection, say. Generally, the way to choose between them has more to do with what the request is *for* than security. Does this request read or change an object, will it be helpful to bookmark/pass to others, etc. etc. [This question has some answers that give some idea of the choices](http://stackoverflow.com/questions/110933/how-should-i-choose-between-get-and-post-methods-in-html-forms). – Matt Gibson Mar 15 '14 at 15:31