[英]Saving the value of radio buttons on click using ajax
Im creating a exam using php. 我使用php创建考试。
My problem is when the page is refresh the checked in the radio button dissappear.. i using ajax to save it in database everytime the radio button is click 我的问题是,刷新页面后,单选按钮中的选中项消失了。.每当单击单选按钮时,我就使用ajax将其保存在数据库中
<?php
session_start();
include './connect.php';
?>
<html>
<head>
</head>
<body>
<div id="online"></div>
<?php
$result = mysql_query("SELECT * FROM questions");
$count = mysql_num_rows($result);
while($rows = mysql_fetch_array($result))
{
?>
<script type="text/javascript" src="jquery.js"></script>
<script>
function saveans(){
var aa = $('input:radio[name=a1]:checked').val();
alert(aa);
$.ajax({
type: "POST",
url: "ans.php",
data: "stdID=1&q=2&ans="+aa,
})
}
</script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$("#online").load("./tmr.php");
var refreshId = setInterval(function() {
//alert("aa");
$("#online").load('./tmr.php');
$.ajax({
type: "POST",
url: "./update.php",
data: "stdID=1"
})
}, 1000);
$.ajaxSetup({ cache: false });
});
</script>
<p><?php echo $rows["questions"]; ?></p>
<input type="radio" id="a<?php echo $rows['questionNum']; ?>" name="a<?php echo $rows['questionNum']; ?>" value="A" onClick="saveans(this);"/>A. <?php echo $rows['choi1']; ?><br/>
<input type="radio" id="a<?php echo $rows['questionNum']; ?>" name="a<?php echo $rows['questionNum']; ?>" value="B" onClick="saveans(this);"/>B. <?php echo $rows['choi2']; ?><br/>
<input type="radio" id="a<?php echo $rows['questionNum']; ?>" name="a<?php echo $rows['questionNum']; ?>" value="C" onClick="saveans(this);"/>C. <?php echo $rows['choi3']; ?><br/>
<input type="radio" id="a<?php echo $rows['questionNum']; ?>" name="a<?php echo $rows['questionNum']; ?>" value="D" onClick="saveans(this);"/>D. <?php echo $rows['choi4']; ?><br/>
<?php
}
?>
</body>
</html>
and the php code is
<?php
include './connect.php';$command = mysql_query("UPDATE ans_1 SET ans".$_POST['q']."='".$_POST['ans']."' WHERE std_id='".$_POST['stdID']."'") or die(mysql_error());
?>
the problem is i what to change the data: "stdID=1&q=2&ans="+aa.. the name of my radio button is a1,a2,a3 for multiple radio buttons.. i want to save it on my database depends the name Is it possible to insert database value in a ajax variable? 问题是我该如何更改数据:“ stdID = 1&q = 2&ans =” + aa ..对于多个单选按钮,我的单选按钮的名称是a1,a2,a3。我想将其保存在数据库中取决于name是否可以在ajax变量中插入数据库值? and the std value is come from session
并且std值来自会话
You define your function as : 您将函数定义为:
function saveans(){
^---no arguments
But invoke it in your onclick handers as: 但是在您的onclick处理程序中将其调用为:
onClick="saveans(this);"
^^^^---passing in "this"
You want: 你要:
function saveans(obj) {
answer = obj.value;
... do ajax ...
}
There's no need to look up by ID (and a hard-coded id at that), since this
and obj
will already be the exact same DOM element you're trying to look up by ID anyways. 无需按ID查找(以及在那里的硬编码ID),因为无论如何,
this
obj
和obj
都已经是您要尝试按ID查找的完全相同的DOM元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.