Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass variable from jQuery to PHP

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'];
}

1 Answers

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']; ?>
like image 172
Dino Babu Avatar answered Jun 12 '26 22:06

Dino Babu