单击复选框保存到数据库

时间:2013-10-29 09:34:03

标签: php html checkbox

GoodDay Masters!

我有一个调查网页,对YES或NO(CheckBox)负责。

我想通过单击CheckBox计算调查并将其保存到数据库中。 这是我的代码......

  <label> Did you find this helpful? </label>
    <input type="radio" name="question" value="yes">YES</input>
    <input type="radio" name="question" value="no">NO</input>
    <?php 
    $fieldname = $_GET['question'];
    if($fieldname == 'YES' ){
        mssql_query("update TBL_SURVEY SET YES = YES + 1 WHERE adddate = (SELECT max(adddate) FROM TBL_SURVEY)");
    }else{
        mssql_query("update TBL_SURVEY SET NO = NO + 1 WHERE adddate = (SELECT max(adddate) FROM TBL_SURVEY)");
    }
?>

但是我遇到错误“通知:未定义的索引:问题”

我应该使用javascript还是ajax?或者我只是混合了我的编码?那我的代码出了什么问题?

如何触发此代码来更新我的数据库?

3 个答案:

答案 0 :(得分:2)

这里未定义索引错误

$fieldname = $_GET['question'];

如果你改变你的代码就不应该发出通知

<?php 
 if(isset($_GET['question'])) { 
    $fieldname = $_GET['question'];
    if($fieldname == 'YES' ){
        mssql_query("update TBL_SURVEY SET YES = YES + 1 WHERE adddate = (SELECT max(adddate) FROM TBL_SURVEY)");
    }else{
        mssql_query("update TBL_SURVEY SET NO = NO + 1 WHERE adddate = (SELECT max(adddate) FROM TBL_SURVEY)");
   }
}
?>

答案 1 :(得分:1)

请试试这段代码。请将PHP代码写入任何函数,并将该函数调用到ajax请求中。

 <input type="checkbox" class="chk" id="question" name="question" value="yes" onclick="get_checbox_data();">YES</input>
<input type="checkbox" class="chk" id="question1" name="question1" value="no" onclick="get_checbox_data();">NO</input>

<script type="text/javascript">
    function get_checbox_data(){

  var ans = 'no';
        if ($('#question').is(':checked')) {
            ans = 'yes';
        }else if ($('#question1').is(':checked')) {
              ans = 'no';
        }
            $.ajax({
          type: "POST",
          url: "SITE_URL/save_checkbox_data",                    
          data:{'question':ans},                 
          success: function (data) {

          }
     });

    }</script>     

Edited:  In php file create function named save_checkbox_data like:    

function save_checkbox_data()
{
    if(isset($_POST['question']) && $_POST['question'] != '')
    {
        $fieldname = $_POST['question'];
        if($fieldname == 'YES' ){
            mssql_query("update TBL_SURVEY SET YES = YES + 1 WHERE adddate = (SELECT max(adddate) FROM TBL_SURVEY)");
        }else{
            mssql_query("update TBL_SURVEY SET NO = NO + 1 WHERE adddate = (SELECT max(adddate) FROM TBL_SURVEY)");
        }
    }
}

祝你好运

答案 2 :(得分:0)

通常,您应使用 POST 而不是 GET 方法发送安全或不可收藏的数据。 确保表单单选按钮有效且具有相同的名称以对其进行分组。

HTML

<form id="myform" action="form_process.php" method="post">
  <div>
    <label>Did you find this helpful? </label>
    <input type="radio" name="question" value="1" />YES
    <input type="radio" name="question" value="0" />NO
    <input type="submit" value="Send" />
 </div>
</form>



PHP form_process.php示例:

<?php
    // Include your database connection code

    if (isset($_POST[question])) {
    // If checkbox was chosen...
        $fieldname = filter_input(INPUT_POST, $_POST['question'], FILTER_VALIDATE_BOOLEAN);
    }

    if ($fieldname == TRUE){
        // Code to run when 'YES' selected
        mssql_query("update TBL_SURVEY SET YES = YES + 1 WHERE adddate = (SELECT max(adddate) FROM TBL_SURVEY)");
    } else if ($fieldname == FALSE) {
        // Code to run when 'NO' selected
        mssql_query("update TBL_SURVEY SET NO = NO + 1 WHERE adddate = (SELECT max(adddate) FROM TBL_SURVEY)");
    } else {
        // Data was not validated correctly
    }
?>


我没有测试这段代码,但这是一般的想法。确保测试查询并检查数据是否已正确保存(未提供数据库架构,代码或信息)。