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

时间:2015-02-26 13:31:07

标签: 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;

}

我真的不知道该怎么做了。对不起,如果我不清楚。

3 个答案:

答案 0 :(得分:1)

我无法清楚地理解您的问题,我发现您的代码存在错误。

<label style="cursor:pointer;">
  <input type="radio" name="rads" value="<?php echo $correct; ?>" id = "radio"><?php echo $answer; ?></label> 
<button onclick="myFunction()">Value</button>';

试试这个。对不起,如果我错了。

答案 1 :(得分:0)

试试这段代码

var try = document.getElementsByName('rads');
   for (var i = 0, len = try.length; i < length; i++) {
   if  !(try[i].checked) {
   // increment wrong in db

   else if (try[i].checked){
    //increment correct in db
  }

  break;

  }

答案 2 :(得分:0)

经过多次试验和错误后,下面的代码终于为我工作了。我添加了ajax函数,单击时单选按钮会调用该函数。

<script>
function addVote(str) {
if (str == "") {
    document.getElementById("txtHint").innerHTML = "";
    return;
} else {
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","try.php?rad="+str,true);
    xmlhttp.send();
    }
}

    </script>

在try.php上,函数可以从中获取单选按钮的值,并根据单击的按钮的值更新数据库。

 <?php
$q = $_GET['rad'];

$con = mysqli_connect('localhost','','','try');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con, 'try');
if($q == 1){
$sql="UPDATE count SET correct = correct+1, wrong=wrong+0 WHERE question_id = 1 ";
} else{
$sql="UPDATE count SET correct=correct+0, wrong=wrong+1 WHERE question_id = 1 ";
}
$result = mysqli_query($con,$sql);


mysqli_close($con);
?>

按钮的onclick以这种方式调用函数,并且应该在名为txtHint的div函数中:

onclick="addVote(this.value)"
相关问题