javascript - 如何将单选按钮的值插入数据库?

标签 javascript php jquery html mysql

我有这个代码:

<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'] enter image description here

每当用户单击单选按钮时,我都会尝试增加数据库中另一个表中的数据。 enter image description here

例如,如果问题是错误的,则该值将为“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/

相关文章:

javascript - 基于 mustache 的逻辑导致 algolia

javascript - 如何将数据传递给 select2 version > 4.0

javascript - 使用错误 : "Uncaught ReferenceError: BitmapDescriptorFactory is not defined" 更改 Google map 图标标记色调颜色

PHP 对 readdir 的权限被拒绝,即使它是相同的用户和组

javascript - 下拉菜单中选定的选项会选择另一个下拉菜单中的选项

jQuery - 单独显示/隐藏具有相同类名的项目

javascript - jQuery 替代 onclick

php - 对于每个循环未正确列出结果

javascript - 一个 php 页面中的两个搜索栏

javascript - 为什么图片没有变化?