我正在尝试创建基于计算机的考试。我使用ajax将用户选择的答案存储为php会话
<script type="text/javascript">
//if the button if check
$( ".form-check-input" ).on( "click", function() {
var checkedvalue = $("input:checked" ).val();
var ques = <?php echo $_SESSION['ques']; ?>;
alert(ques);
$.ajax({
url: "ajax.php",
method: "POST",
data: {
ques:ques,
checkedvalue:checkedvalue,
request:"add"
}
}).done(function(count) {
$( "#log" ).html(checkedvalue + " is checked2!" ).show();
$( "#log2" ).html(count + " is checked2!" ).show();
// setTimeout(hideMessage, 2000);
});
});
//$( ".form-check-input" ).attr("checked",false).html("is checked2!" ).show();
</script>
这是ajax.php脚本
<?php
// START THE SESSION
session_start();
// CONFIGURATION
require("db.php");
$conn=new mysqli ($dbServername, $dbusername, $dbpassword, $dbname);
// PROCESS REQUESTS
switch ($_POST['request']) {
case "add":
// ITEMS WILL BE STORED IN THE ORDER OF
// $_SESSION['cart'][PRODUCT ID] = QUANTITY
if ($_POST['checkedvalue'] ==TRUE) {
$_SESSION['option'][$_POST['ques']][$_POST['checkedvalue']] = 1;
echo '<pre>';
var_dump($_SESSION['option'][$_POST['ques']][$_POST['checkedvalue']]);
echo '</pre>';
echo $_SESSION['option'][$_POST['ques']][$_POST['checkedvalue']];
}
//here i am trying to checkif the user selects another answer which has been stored in the session so that it can remove the previously selected answer in the session
elseif($_SESSION['option'][$_POST['ques']][$_POST['checkedvalue']]>1){
unset($_SESSION['option'][$_POST['ques']][$_POST['checkedvalue']]);
}else {
echo 'yes';
}
break;
此问题是,如果用户取消选中答案,则php会话仍会保存,但我无法访问会话数组中的特定答案以取消设置该特定答案,然后按问题将新选择的答案保存在php会话中
htm
<?php
session_start();
require("db.php");
?>
<!DOCTYPE HTML>
<HTML>
<Head>
<title>Examination Application</title>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="style.css" rel="stylesheet">
<!-- Bootstrap core JavaScript -->
</head>
<script src="jquery/jquery.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
<body>
<?php// require 'opera.php' ?>
<?php
if(!isset($_SESSION['user']))
{
echo '<a href="?username=moses&page=1"><button class="btn btn-danger">START QUIZ</button></a>';
$_SESSION['user'] =$_GET['username'];
}else{
// if(isset($_POST['submit']))
// {
// // $submit=='Next Question' && isset($_POST['OPTION_A'])
// $ans= $_POST['optradio'];
// $ques= $_POST['ques'];
// echo $ques;
// echo $ans;
// echo session_id();
// $insertans = $conn->query("insert into useranswers(q_num, ans, session_id) values('$ques', '$ans','".session_id()."')") ;
// if($insertans == TRUE){
// echo 'inserted';
// }else{
// echo 'not inserted buddy';
// }
// }else{
// echo 'yes';
// }
// //unset($_SESSION['user']);
// //session_destroy();
// CONFIGURATION
$url = $_GET['page'];
echo $url;
if($_GET['page']==""){
//header("location:index.php");
$num = 1;
}else{
$num = $_GET['page'];
}
$sql1 = "Select * from questions WHERE '$num' = q_num";
$query_count=$conn->query($sql1);
$res =$conn->query($sql1);
while($row = $res->fetch_assoc()){
$_SESSION['ques'] = $row['q_num'];
// echo '<form name="myform" method="POST" action="">';
echo '<input type="hidden" name="ques" value="'.$row['q_num'].'"> ';
echo '<div style="font-size:40px;padding:20px; margin-top:40px;margin-left:55px; margin-right:40px; margin-right:40px;" >'.$row['q_num'].'.'.$row['ques'].'</div>';
?>
<div class="form-check-inline">
<label class="form-check-label">
<input type="radio" class="form-check-input" name="optradio" value="<?php echo $row['OPTION_A'];?>" <?php echo 'checked';?> ><?php echo $row['OPTION_A'];?>
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label">
<input type="radio" class="form-check-input" name="optradio" value="<?php echo $row['OPTION_B'];?>" ><?php echo $row['OPTION_B'];?>
</label>
</div>
<div class="form-check-inline ">
<label class="form-check-label">
<input type="radio" class="form-check-input" name="optradio" value="<?php echo $row['OPTION_C'];?>" ><?php echo $row['OPTION_C'];?>
</label>
</div>
<div class="form-check-inline ">
<label class="form-check-label">
<input type="radio" class="form-check-input" name="optradio" value="<?php echo $row['OPTION_D'];?>"><?php echo $row['OPTION_D'];?>
</label>
</div>
<?php
}
echo '<div id="log"></div>';
echo '<div id="log2"></div>';
if(isset($_SESSION['option']))
{
echo '<pre>';
var_dump($_SESSION['option']);
echo '</pre>';
echo '<pre>' . print_r($_SESSION['option'], TRUE) . '</pre>';
}else{
echo 'nothing yet';
}
}
?>
答案 0 :(得分:0)
请尝试在复选框上设置绑定更改事件,而不是单击事件。复选框的单击事件中,复选框总是会收到相同的结果(无论选中还是未选中)。
$(".form-check-input").change(function() {
// Perform ajax submission here
$.ajax({
url: "ajax.php",
method: "POST",
data: {
ques:ques,
checkedvalue: this.checked,
request:"add"
}
});
});
然后在php端,您可以根据上面的“ checked”变量将适当的值更新到会话中。
if ($_POST["checkedvalue"]) {
// Question is check
} else {
// Question not check
}