我不能让下面的PHP + jQuery的工作 - 所有我想要的脚本做的是通过Ajax传递价值,并获得PHP的抓住它,检查它匹配,并且加1分.
这是我写的代码:
<?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>
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助,我在两个实例中都将GET更改为POST,并且没有任何乐趣 - 还有其他错误.
Jua*_*des 17
首先:不要回到黑暗时代......不要使用相同的脚本来生成HTML并响应ajax请求.
我无法理解你想要做什么......让我改变你的代码,这样至少可以理解并记录正在发生的事情.似乎问题在于您从成功处理程序调用location.reload.
// ajax.php - 如果name参数是145则输出2,否则输出1(????)
<?php
$score = "1";
$userAnswer = $_POST['name'];
if ($_POST['name'] == "145"){
$score++;
}
echo $score;
?>
Run Code Online (Sandbox Code Playgroud)
// test.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>
Run Code Online (Sandbox Code Playgroud)