dpvmjk0479 2015-02-26 13:31
浏览 342
已采纳

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

I have this code:

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

where $correct is a row in my database that shows if a question is correct or wrong and has a value of either 1 or 0 ($correct = $row['correct'] enter image description here

I'm trying to increment data in another table in the database anytime a user clicks on a radio button. enter image description here

For example, if a question is wrong the value will be '0' and when a user clicks on it, the 'wrong' row will be incremented and vice-versa.

I tried using PHP to insert the values:

$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") ;
               }

but only the 'wrong' column was incrementing. I tried using js to print out the value of the selected radio button:

<p id =" demo"></p>
<script>
function myFunction() {
    var x = document.getElementById("radio").value;
    document.getElementById("demo").innerHTML = x;
}
</script>

But nothing appeared when I clicked the button. I also tried:

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;

}

I'm not really sure what to do anymore. Sorry if I'm not clear.

  • 写回答

3条回答 默认 最新

  • doqrt26664 2015-03-06 04:14
    关注

    The code below finally worked for me after much trials and error. I added the ajax function below which the radio buttons call when clicked.

    <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>
    

    On try.php, which the function works from takes the value of the radio button and updates the database according to the value of the button clicked.

     <?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);
    ?>
    

    The button's onclick calls the function this way and should be in a div function called txtHint:

    onclick="addVote(this.value)"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?