0

好日子大师!

我有一个回答是或否(复选框)的调查网页。

我想通过单击 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?或者我只是混合了我的编码?那么我的代码有什么问题?

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

4

3 回答 3

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)");
   }
}
?>
于 2013-10-29T09:44:04.940 回答
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)");
        }
    }
}

祝你好运

于 2013-10-29T10:16:57.463 回答
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
    }
?>


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

于 2013-10-30T11:18:45.097 回答