gpt4 book ai didi

PHP + Jquery - 通过 ajax 将值传递给 php 并检查变量

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:53:35 27 4
gpt4 key购买 nike

我无法让以下 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>

感谢您的帮助,我在这两种情况下都将 GET 更改为 POST,但没有任何乐趣 - 还有其他问题。

最佳答案

首先:不要回到黑暗时代...不要使用相同的脚本来生成 HTML 和响应 ajax 请求。

我无法理解您正在尝试做什么...让我更改您的代码,使其至少有一定意义并记录正在发生的事情。似乎问题在于您正在从成功处理程序调用 location.reload。

//ajax.php - 如果名称参数为 145,则输出 2,否则输出 1 (?????)

<?php
$score = "1";
$userAnswer = $_POST['name'];
if ($_POST['name'] == "145"){
$score++;
}
echo $score;
?>

//测试.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>

关于PHP + Jquery - 通过 ajax 将值传递给 php 并检查变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4218063/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com