3

I created a script to read a hash (www.website.com/#hash) and pass the hash in to php. The alert(hash) pops up with the hash value, but the hash is not being echoed out in the post variable. Any idea why?

jQuery page

<script>
    var hash = location.hash;
    $.post("community.php", {
        hash: hash
    });
    alert(hash);
</script>

Community.php page

<?php echo $_POST['hash']; ?>



Edits - $_GET below was originally $_POST above.

I have a foreach that is looping through postings (posting IDs) in a function. I need to pass the HASH into the function and compare it to the posting IDs everytime. Only problem is, the $_GET['hash'] will not show up inside the function.

function something() {
   echo $_GET['hash'];
}
  • How do you know that it is not echoed? Your code should work just fine – Viktor S. Apr 01 '13 at 10:49
  • @FAngel Because `` did not echo anything lol –  Apr 01 '13 at 10:53
  • Ok. Let me rephrase my question - where do you expect to see result of echo? Have you checked network tab in developer tools or firebug and found that response is empty? Or you expect to see it somewhere on page? Or have you tried to debug PHP and found that $_POST["hash"] is empty? – Viktor S. Apr 01 '13 at 10:55
  • 1
    @ChristianRankin what version of jQuery are you using? – LoneWOLFs Apr 01 '13 at 10:55
  • @LoneWOLFs, the newest version. –  Apr 01 '13 at 10:56
  • @FAngel, I haven't done those yet, but it appears it is sending it over.. it's just not echoing it out in php. –  Apr 01 '13 at 11:09
  • @ChristianRankin check my answer u'd understand what's happening – LoneWOLFs Apr 01 '13 at 11:10
  • Well... You did nothing from things listed above, and you know that it is not echoing? How? Actually, as far as I understand, LoneWOLFs already answered your question. – Viktor S. Apr 01 '13 at 11:11
  • Code in update is an actual code which is not working? – Viktor S. Apr 01 '13 at 11:25
  • He did answer my question, and I'm pretty sure I said `I haven't done those yet` –  Apr 01 '13 at 11:26
  • @FAngel, The $_GET['hash'] is what I was pulling from jQuery. It's working outside a function, but not inside. –  Apr 01 '13 at 11:26
  • @ChristianRankin updated my answer and i'd recommend you to go through some basic ajax tutorials to really understand this. – LoneWOLFs Apr 01 '13 at 11:33
  • @ChristianRankin what's db_open();? – LoneWOLFs Apr 01 '13 at 11:36
  • @LoneWOLFs, those are my custom MySQLi functions, same thing with db_query. It saves a lot of time. Also, I know ajax well, and my original script worked, just not showing up on php page. –  Apr 01 '13 at 11:38
  • @ChristianRankin if you are using objects of mysqli inside those custom functions you need to declare those mysqli objects global inside the function or recreate them inside the function for them to work. – LoneWOLFs Apr 01 '13 at 11:45
  • @LoneWOLFs, I may be a n00b in ajax, but not PHP. –  Apr 01 '13 at 11:46
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27322/discussion-between-christian-rankin-and-lonewolfs) –  Apr 01 '13 at 11:46

5 Answers5

3

use ajax like this, send the value in hash

  function send_hash(hash) {
    $.ajax({
      url   : "community.php", 
      type  : "POST",
      cache : false,
      data  : {
        hash : hash
      }
    });
  }

now u will get

<?php echo $_POST['hash']; ?>
Dino Babu
  • 5,814
  • 3
  • 24
  • 33
3
<script>
    var hash = location.hash;
    $.post("community.php", {
        hash: hash
    }, function(responde){ alert(responde); });
</script>

To check what your PHP response :)

Svetoslav
  • 4,686
  • 2
  • 28
  • 43
2

$.post is an ajax event. You are posting data by ajax so by that the page doesn't go to the communitypage.php. For the behaviour you want you have to do normal form post and not ajax post. $.post will retrive anything you echo on communitypage.php using this code.

//Note the 3rd argument is a callback.
var hash = location.hash;
$.post('communitypage.php',{hash:hash},function(data){
    alert(data);
});

Process hash on that page and alert something if u find the hash. You'd find the echo in the data

You could mod the communitypage.php to return html as below

<?php
if($isset($_POST["hash"]) && strlen(trim($_POST["hash"])) > 0){
    echo "Hash found :: " . $_POST["hash"]; 
}else
    echo "No hash found";
?>

Note this will return html you can modify the response to return xml, json as per your needs.

Refer jQuery post() documentation for more info.

For the updated question

It works outside because it is called when the ajax call is made inside a function it won't because the function is not called. If you want to do that (and i don't know why) u can do like this.

function myfunction(){
    //your code goes here
}

myfunction(); //call the function so that the code in the function is called upon

Though that's an overkill unless u want to call the function over and over in the same script. If it works without a function you should do it that way.

LoneWOLFs
  • 2,306
  • 3
  • 20
  • 38
  • Let me know if you see what I'm talking about. –  Apr 01 '13 at 11:32
  • @ChristianRankin already answered i think. update me if u didn't get it. – LoneWOLFs Apr 01 '13 at 11:34
  • I have a foreach that is looping through postings (posting IDs) in a function. I need to pass the HASH into the function and compare it to the posting IDs everytime, that is why. Only problem is, the $_GET['hash'] will not show up inside the function. –  Apr 01 '13 at 11:42
1
  <script>
  var hash = location.hash;
   //Try this.
   window.location.href = 'community.php?val='+hash;
  </script>

OR

   <script>
   $.post("community.php","val="+hash);
   </script>

In community.php

     $res=isset($_REQUEST['val'])?$_REQUEST['val']:'';

hope it will give you some solution.

Nirmal
  • 2,340
  • 21
  • 43
0

but the hash is not being echoed out in the post variable

This is because if you want to send it when page loads then you have to use this in doc ready handler:

<script>
  $(function(){
     var hash = location.hash;
     $.post("community.php", { hash: hash });
     alert(hash);
  });
</script>
Jai
  • 74,255
  • 12
  • 74
  • 103