doudouxuqh198138 2015-10-27 12:42
浏览 62

使用ajax和php从多个复选框和其他控件保存数据

i Have a trouble in saving my data from check boxes. i have tried many ways but i do not get the result should any one please help me in the case? this is my main page.

    <!DOCTYPE html>
<html>
<head>
    <link href="css/bootstrap.min.css" type="text/css" rel="stylesheet">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Loading data into Bootstrap Framework Modal</title>
</head>
<body>
    <a class="showMathQuestions btn btn-warning btn-sm" data-toggle="modal" data-target="#myModal">Math</a>
    <a class="showAlsanaQuestions btn btn-warning btn-sm" data-toggle="modal" data-target="#myModal">Dari</a>
    <a >Pashto</a>
    <a >Geometry</a>
    <a >History</a> 
<div class="modal fade" id="myModal" tabsindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">Select Questions</h4>
        </div>
            <div class="modal-body">
                    <div class="form-group">
                        <div id="viewdata">

                        </div>
                    </div>
        <div class="modal-footer">
            <button type="button"  class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" onclick="submitForm()" id="save" class="btn btn-primary">Save Changes</button>
        </div>
        </div>
    </div>
</div>
<div id="info"></div>

    <script type="text/javascript" src="js/jquery-1.11.2.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
     <script>
        $('.showMathQuestions').on('click' , function() {
            $.ajax({
                type: "GET",
                url: "getMathQuestionsForModal.php"
            }).done(function(data)
            {
                $('#viewdata').html(data);
            });
        }); 
     </script>
</body>
</html>

this is my modal which im retrieving data ..

<form name="form1" method="POST" action="" id="form1">
        <label for="date">Date</label>
                                <input type="text" class="form-control" value="" id="date" />
                            </div>
                            <div class="from-group">
                                <label for="number">Number</label>
                                <input type="text" class="form-control" id="number" />
                            </div>
                            <div><br /></div>
        <table class="table table-bordered table-hover" >
        <thead>
            <tr>
                <th>Question ID</th>
                <th>Question</th>
                <th>Answers</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
    <?php
        //Require Connection to do attractioin with database
        require('connection.php');
        //writing query and storing in query variable
        $query = "SELECT question.q_id, question.qps, answers.q_id, answers.ps1, answers.ps2, answers.ps3, answers.ps4 FROM question,answers WHERE Subject = 'ریاضیات' and question.q_id = answers.q_id";
        $executeQuery = $mysqli->query($query);
        while ($row = $executeQuery->fetch_object()) {
    ?>
            <tr>
                <td width="10%"><input style="width: 80px; background-color: #FFFFFF; border-style:none;" name="question_id" value="<?php echo $row->q_id; ?>" disabled /></td>
                <td width="60%"><textarea style="background-color: #eeeeee; border-style:none;" disabled ><?php echo $row->qps; ?></textarea></td>
                <td width="20%">
                    <div class="col-md-2">الف: <?php echo $row->ps1; ?></div>
                    <div class="col-md-2">ب: <?php echo $row->ps2; ?></div>
                    <div class="col-md-2">ج: <?php echo $row->ps3; ?></div>
                    <div class="col-md-2">د: <?php echo $row->ps4; ?></div>
                </td>
                <td width="20%">
                    <input type="checkbox" name="question[]" id="question" value="<?php echo $row->qps; ?>" />
                    <label for="check">Select</label>
                </td> 
            </tr>
    <?php 
        }
    ?>
        </tbody>
    </table>
</form>

after retrieving data again i want to store data with selected check boxes in other table of data base which is booklet. by this two methods i tried.

<script>
    function submitForm(){
          var question = new Array();
          $("input.checked").each(function() {
            data['question[]'].push($(this).val());
          });
          $.ajax({
            type: "POST",
            url: "saveBookletData.php",
            data : {date:$("#date").val(),
                    number:$("#number").val(),
                    question:question },
            success: function(data){
                $('#info').html(data);
            }
          });
          return false;
    }
</script> 

and

<script>
    function submitForm(){
        var form = document.form1;
        var dataString = $(form).serialize();

        $.ajax({
            type: 'POST',
            url: 'saveBookletData.php',
            data: dataString,
            success: function(data){
                $('#info').html(data);
            }
        });
        return false;
    }
</script>

and this is my saveBookletData php code.

<?php
    include "connection.php";
    if(isset($_POST['question'])){
        $date = $_POST['date'];
        $number = $_POST['number'];
        $question = $_POST['question'];

        foreach ($quesion as $key => $value) {
            $query = "insert into booklet (datas, bookletNumber, questions) values ('".$date."', '".$number."', '".$value."')";
            if($mysqli->query($query)){
                echo "<script>alert('Booklet Data has been saved');</script>";
            }else{
                echo "<script>alert('Booklet Data not saved');</script>";
            }
        }

    }
?>

but this two methods do not save data in database along selected checkboxes . should any one please help me in this case.. i will really appreciate your help.

  • 写回答

1条回答 默认 最新

  • donglu5728 2015-10-27 12:50
    关注

    You are using insertkj in you query instead of insert. Also try to debug AJAX requests using Chrome Developers Tool or Firebug.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大