javascript 将变量从 jQuery 传递给 PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15741937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass variable from jQuery to PHP
提问by
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?
我创建了一个脚本来读取哈希(www.website.com/#hash)并将哈希传递给 php。在alert(hash)
与哈希值弹出,但在后变量的散列没有被回波输出。知道为什么吗?
jQuery page
jQuery 页面
<script>
var hash = location.hash;
$.post("community.php", {
hash: hash
});
alert(hash);
</script>
Community.php page
Community.php 页面
<?php echo $_POST['hash']; ?>
Edits- $_GET below was originally $_POST above.
编辑- 下面的 $_GET 最初是上面的 $_POST。
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.
我有一个 foreach,它在一个函数中循环发布(发布 ID)。我需要将 HASH 传递到函数中,并将其与每次发布的 ID 进行比较。唯一的问题是,$_GET['hash'] 不会出现在函数内部。
function something() {
echo $_GET['hash'];
}
采纳答案by LoneWOLFs
$.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.
$.post 是一个 ajax 事件。您通过 ajax 发布数据,因此该页面不会转到 communitypage.php。对于您想要的行为,您必须进行正常的表单发布而不是 ajax 发布。$.post 将使用此代码检索您在 communitypage.php 上回显的任何内容。
//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
在该页面上处理哈希并在您找到哈希时发出警报。你会发现回声data
You could mod the communitypage.php
to return html as below
你可以修改communitypage.php
返回 html 如下
<?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.
请注意,这将返回 html,您可以根据需要修改响应以返回 xml、json。
Refer jQuery post() documentationfor more info.
有关更多信息,请参阅jQuery post() 文档。
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.
它在外部工作,因为在函数内部进行 ajax 调用时会调用它,因为未调用该函数,因此不会调用。如果你想这样做(我不知道为什么)你可以这样做。
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.
尽管这是一种矫枉过正,除非您想在同一个脚本中一遍又一遍地调用该函数。如果它在没有函数的情况下工作,你应该这样做。
回答by Dino
use ajax like this, send the value in hash
像这样使用ajax,以散列形式发送值
function send_hash(hash) {
$.ajax({
url : "community.php",
type : "POST",
cache : false,
data : {
hash : hash
}
});
}
now u will get
现在你会得到
<?php echo $_POST['hash']; ?>
回答by Svetoslav
<script>
var hash = location.hash;
$.post("community.php", {
hash: hash
}, function(responde){ alert(responde); });
</script>
To check what your PHP response :)
检查您的 PHP 响应:)
回答by Nirmal
<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
在 community.php
$res=isset($_REQUEST['val'])?$_REQUEST['val']:'';
hope it will give you some solution.
希望它会给你一些解决方案。
回答by Jai
but the hash is not being echoed out in the post variable
但散列没有在 post 变量中被回显
This is because if you want to send it when page loads then you have to use this in doc ready
handler:
这是因为如果您想在页面加载时发送它,那么您必须在doc ready
处理程序中使用它:
<script>
$(function(){
var hash = location.hash;
$.post("community.php", { hash: hash });
alert(hash);
});
</script>