我创建了一个脚本来读取散列(www.website.com/#散列)并将哈希传递给php。alert(hash)将弹出哈希值,但不会在post变量中回显哈希。知道为什么吗?
jQuery页面
<script>
var hash = location.hash;
$.post("community.php", {
hash: hash
});
alert(hash);
</script>Community.php页面
<?php echo $_POST['hash']; ?>编辑的- $_GET下面最初是$_POST之上。
我有一个预见,它循环在函数中的帖子(张贴in )中。我需要将散列传递到函数中,并每次将其与投递ID进行比较。唯一的问题是,$_GET‘散列’将不会出现在函数中。
function something() {
echo $_GET['hash'];
}发布于 2013-04-01 11:02:57
$.post是ajax事件。您是通过ajax发布数据的,这样页面就不会转到communitypage.php了。对于您想要的行为,您必须做普通形式的post,而不是ajax post。$.post将使用此代码检索communitypage.php上的任何回显内容。
//Note the 3rd argument is a callback.
var hash = location.hash;
$.post('communitypage.php',{hash:hash},function(data){
alert(data);
});处理该页面上的散列,如果找到哈希,请发出警告。你会在data中找到回声
您可以将communitypage.php修改为返回html,如下所示
<?php
if($isset($_POST["hash"]) && strlen(trim($_POST["hash"])) > 0){
echo "Hash found :: " . $_POST["hash"];
}else
echo "No hash found";
?>注意,这将返回html,您可以根据需要修改响应以返回xml,json。
有关更多信息,请参考jQuery post文档。
用于更新的问题
它在外部工作,因为当ajax调用是在函数内部调用时,它就会被调用,而它不会被调用,因为函数没有被调用。如果你想这么做(我不知道为什么),你可以这样做。
function myfunction(){
//your code goes here
}
myfunction(); //call the function so that the code in the function is called upon不过,除非您想在同一个脚本中一次又一次地调用该函数,否则这是过火了。如果它在没有函数的情况下工作,你应该这样做。
发布于 2013-04-01 10:45:36
像这样使用ajax,在散列中发送值
function send_hash(hash) {
$.ajax({
url : "community.php",
type : "POST",
cache : false,
data : {
hash : hash
}
});
}现在你会得到
<?php echo $_POST['hash']; ?>发布于 2013-04-01 10:45:51
<script>
var hash = location.hash;
$.post("community.php", {
hash: hash
}, function(responde){ alert(responde); });
</script>检查PHP响应:)
https://stackoverflow.com/questions/15741937
复制相似问题