gpt4 book ai didi

php - 如何从 json 获取值并访问它的基本方法

转载 作者:行者123 更新时间:2023-12-01 04:52:10 26 4
gpt4 key购买 nike

这就是我使用ajax的方式

$.get('checkanswer.php',{'clickedvalue':clickedvalue,'qid':qid},function(data){
$this.find(".report").html(data);

这是我的 PHP 代码,数据来自于此

<?php
$countresult=0;
$questionid=$_GET['qid'];
$answer=$_GET['clickedvalue'];
$dbconnect=mysqli_connect('localhost','root','','quiz')or die("Error Connecting to database");
$query="select answer from question_answer where id=$questionid";
$result=mysqli_query($dbconnect,$query);
while($rows=mysqli_fetch_array($result))
{
$dbanswer=$rows['answer'];
}
if($dbanswer==$answer)
{
echo "Correct";
$countresult=$countresult+1;
}
else{
echo "Incorrect";
$countresult=$countresult-1;
}

?>

现在,以前我只是检查结果是否正确并显示结果,但现在我希望 PHP 页面甚至返回存储 $countresult 中存储的计数的变量。我知道我必须使用 json,但如何在 PHP 页面中使用它,传递值并从另一个页面访问该值,

最佳答案

在你的 PHP 中:

$data = array('countresult' => $countresult);
$str = json_encode($data);
echo $str;

在你的js中:

$.get('checkanswer.php',{'clickedvalue':clickedvalue,'qid':qid},function(data){
alert(data['countresult']);
}, "json");

关于 jQuery.get() 的文档

关于php - 如何从 json 获取值并访问它的基本方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18379695/

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