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

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

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 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示