PHP + Jquery - 通过 ajax 将值传递给 php 并检查变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4218063/
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
PHP + Jquery - pass value through ajax to php and check against variable
提问by Richard
I can't get the following PHP + jQuery to work - all I want the script to do is pass the value through ajax, and get the php to grab it, check it matches and add 1 to score.
我无法让以下 PHP + jQuery 工作 - 我想让脚本做的就是通过 ajax 传递值,然后让 php 抓取它,检查它是否匹配并添加 1 来得分。
This is the code I've written:
这是我写的代码:
<?php
$score = "1";
$userAnswer = $_POST['name'];
if ($_POST['name'] == "145"){
$score++;
}else{
//Do nothing
}
echo $score;
?>
<script type="text/javascript">
$(document).ready(function() {
$("#raaagh").click(function(){
var value = "145";
alert(value);
$.ajax({
url: 'processing.php', //This is the current doc
type: "POST",
data: ({name: value}),
success: function(){
location.reload();
}
});
});
});
</script>
<p id="raaagh">Ajax Away</p>
Thanks for the help, I've changed GET to POST in both instances, and no joy - there's something else wrong.
感谢您的帮助,我在这两种情况下都将 GET 更改为 POST,但并不高兴 - 还有其他问题。
回答by Juan Mendes
First of all: Do not go back to the dark ages... don't use the same script to generate HTML and to respond to an ajax request.
首先:不要回到黑暗时代……不要使用相同的脚本来生成 HTML 和响应 ajax 请求。
I can't make any sense of what you are trying to do... Let me change your code so it at least makes some sense and document what's going on. It seems like the problem is the fact that you are calling location.reload from your success handler.
我无法理解您正在尝试做什么...让我更改您的代码,使其至少有意义并记录正在发生的事情。问题似乎在于您从成功处理程序调用 location.reload 。
// ajax.php - Outputs 2 if the name parameter is 145, 1 otherwise (????)
// ajax.php - 如果 name 参数为 145,则输出 2,否则输出 1 (????)
<?php
$score = "1";
$userAnswer = $_POST['name'];
if ($_POST['name'] == "145"){
$score++;
}
echo $score;
?>
// test.html
// 测试.html
<script type="text/javascript">
$(document).ready(function() {
$("#raaagh").click(function(){
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
data: ({name: 145}),
success: function(data){
// Why were you reloading the page? This is probably your bug
// location.reload();
// Replace the content of the clicked paragraph
// with the result from the ajax call
$("#raaagh").html(data);
}
});
});
});
</script>
<p id="raaagh">Ajax Away</p>
回答by yossi
回答by Shoe
Replace $_GET
with $_POST
and there you are.
Basically POST and GET are two different way to pass variables to a script. The get method in php can also be attached at the end of a url : script.php?variable=value
and it is really easy to hack. While the post method can be submitted with forms or ajax calls and it is pretty safe, at least more than the get.
Also i'd suggest you to check whatever a GET or POST variable is set before calling it, so that you can prevent stupid notice errors.
替换$_GET
为$_POST
,你就在那里。基本上 POST 和 GET 是将变量传递给脚本的两种不同方式。php 中的 get 方法也可以附加在 url 的末尾:script.php?variable=value
而且它真的很容易被破解。虽然 post 方法可以通过表单或 ajax 调用提交,而且它非常安全,至少比 get 更安全。此外,我建议您在调用之前检查设置了 GET 或 POST 变量的任何内容,以便您可以防止愚蠢的通知错误。
Just use the following code:
只需使用以下代码:
if (isset($_POST['var']) and !empty($_POST['var'])) { // do something }
You can also delete the
也可以删除
}else{
// do nothing
}
part of the script, since the else clause it is not necessary always.
脚本的一部分,因为 else 子句并不总是必要的。
回答by EAMann
You're submitting the data with an Ajax POST, but trying to read it out of a GET. Either use type: "GET"
in your Ajax call or $_POST['name']
in your PHP.
您使用 Ajax POST 提交数据,但尝试从 GET 中读取它。无论是用type: "GET"
在你的Ajax调用或$_POST['name']
在你的PHP。
回答by superfro
The problem is you are using jQuery to POST your value, yet you are reading it with GET.
问题是您正在使用 jQuery 发布您的值,但您正在使用 GET 读取它。
You should be able to fix your problem by changing your $_GET['name'] to $_POST['name']
您应该能够通过将 $_GET['name'] 更改为 $_POST['name'] 来解决您的问题