javascript - Pass variable from jQuery to PHP -
i created script read hash (www.website.com/#hash) , pass hash in php. alert(hash)
pops hash value, hash not being echoed out in post variable. 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 $_post above.
i have foreach looping through postings (posting ids) in function. need pass hash function , compare posting ids everytime. problem is, $_get['hash'] not show inside function.
function something() { echo $_get['hash']; }
$.post ajax event. posting data ajax page doesn't go communitypage.php. behaviour want have normal form post , not ajax post. $.post retrive echo on communitypage.php using code.
//note 3rd argument callback. var hash = location.hash; $.post('communitypage.php',{hash:hash},function(data){ alert(data); });
process hash on page , alert if u find hash. you'd find echo in data
you mod communitypage.php
return html below
<?php if($isset($_post["hash"]) && strlen(trim($_post["hash"])) > 0){ echo "hash found :: " . $_post["hash"]; }else echo "no hash found"; ?>
note return html can modify response return xml, json per needs.
refer jquery post() documentation more info.
for updated question
it works outside because called when ajax call made inside function won't because function not called. if want (and don't know why) u can this.
function myfunction(){ //your code goes here } myfunction(); //call function code in function called upon
though that's overkill unless u want call function on , on in same script. if works without function should way.
Comments
Post a Comment