作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这个代码:
<label style="cursor:pointer;"><input type="radio" name="rads" value="'.$correct.'" id = "radio">'.$answer.'</label>
<button onclick="myFunction()">Value</button>';
$correct 是我数据库中的一行,它显示问题是正确的还是错误的,其值为 1 或 0
($correct = $row['correct']
每当用户单击单选按钮时,我都会尝试增加数据库中另一个表中的数据。
例如,如果问题是错误的,则该值将为“0”,当用户点击它时,“错误”行将递增,反之亦然。
我尝试使用 PHP 插入值:
$vote = isset($_GET['rads']);
if($vote == 1){
$sql = mysqli_query($connection, "UPDATE stats SET question_id='$question_id', correct=correct + 1, wrong=0") ;
} elseif($vote == 0) {
$sql = mysqli_query($connection, "UPDATE stats SET question_id='$question_id', correct=0, wrong=wrong + 1") ;
}
但只有“错误”列在递增。我尝试使用 js 打印出所选单选按钮的值:
<p id =" demo"></p>
<script>
function myFunction() {
var x = document.getElementById("radio").value;
document.getElementById("demo").innerHTML = x;
}
</script>
但是当我点击按钮时什么也没有出现。我也试过:
var try = document.getElementsByName('rads');
for (var i = 0, len = try.length; i < length; i++) {
if (try[i].checked == 0) {
// increment wrong in db
else if (try[i].checked == 1){
//increment correct in db
}
break;
}
我真的不知道该做什么了。对不起,如果我不清楚。
最佳答案
我无法清楚地理解你的问题,我从你的代码中发现了一个错误。
<label style="cursor:pointer;">
<input type="radio" name="rads" value="<?php echo $correct; ?>" id = "radio"><?php echo $answer; ?></label>
<button onclick="myFunction()">Value</button>';
试试这个。对不起,如果我错了。
关于javascript - 如何将单选按钮的值插入数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28743823/
我是一名优秀的程序员,十分优秀!