插入值时不检查复选框?

时间:2017-03-20 10:55:26

标签: javascript php jquery html5 mysqli

代码:

<script>
  $(document).ready(function(){
    $('#save_exam').click(function(){
      var student_id=$('#student_id').val();
      var student_firstname=$('#student_firstname').val();
      var student_lastname=$('#student_lastname').val();
      var exam_name=$('#exam_name').val();
      var date=$('#date').val();

      window.location=window.location.href+'&student='+student_id+'&student_firstname='+student_firstname+'&student_lastname='+student_lastname+'&exam_name='+exam_name+'&date='+date
    });
  });
</script>

查询:

<?php 
    $sql = "select * from students where student_id = '$student_id'";
    $result = mysqli_query($link,$sql);
    while ($row = mysqli_fetch_array($result)) 
    {
      $studentid = $row['student_id'];
      $student_firstname = $row['firstname'];
      $student_lastname = $row['lastname']; 
    }

    if(isset($_GET['student_firstname']))
      {
        extract($_GET);
        $query = "select * from student_exams where exam_name = '$exam_name' and student_id ='$student_id'";
        $result = mysqli_query($link,$query);
        $row = mysqli_fetch_array($result);
        if($row > 0 )
        {
          $msg .="<h5 style='text-align:center;color:red;'>Your Exam Detail Already Exist. Please Select Other Exam.</h5>";
        }
        else
        {
            $sql = "insert into student_exams(student_id,student_firstname,student_lastname,exam_name,date)values('$student_id','$student_firstname','$student_lastname','$exam_name','$date')";
            $result=mysqli_query($link,$sql);
            if($result == true)
            {
            $msg .="<h4 style='text-align:center;color:green;'>Your Data Has Been Submitted Successfully.</h4>";

            }
            else
            {
              $msg .="<h4 style='text-align:center;color:red;'>Error</h4>";
            }
        }
      }
    ?>

html代码:

<form method="post">
    <?php
      if($studentid == '')
      {
        echo '';
      }
      else
      {
    ?>
    <input type="checkbox" name="save_exam" id="save_exam">&nbsp;&nbsp;<b style="color: #337ab7;">Mark And Save This Exam</b>
    <?php
      }
    ?>
    <input type="hidden" name="student_id" id="student_id" value="<?php echo $student_id; ?>">
    <input type="hidden" name="student_firstname" id="student_firstname" value="<?php echo $student_firstname; ?>">
    <input type="hidden" name="student_lastname" id="student_lastname" value="<?php echo $student_lastname; ?>">
    <input type="hidden" name="exam_name" id="exam_name" value="<?php echo $exam_name; ?>">
    <input type="hidden" name="date" id="date">
  </form>

这里当我检查复选框页面重新下载并将值插入数据库并显示消息,即您的数据已成功提交。但复选框不是检查。那么,我们怎么做呢?

谢谢

2 个答案:

答案 0 :(得分:0)

您没有使用选中=&#39;已选中&#39;属性。 请添加checked =&#39;已选中&#39;在复选框属性中,如

<input type="checkbox" name="save_exam" checked='checked' id="save_exam">&nbsp;&nbsp;<b style="color: #337ab7;">Mark And Save This Exam</b>

答案 1 :(得分:0)

将您的输入更改为:

<input type="checkbox" name="save_exam" id="save_exam" <?php session_start(); if($_SESSION['saved']=="true"){echo "checked";} ?>>&nbsp;&nbsp;<b style="color: #337ab7;">Mark And Save This Exam</b>

查询代码:

<?php session_start();
    $sql = "select * from students where student_id = '$student_id'";
    $result = mysqli_query($link,$sql);
    while ($row = mysqli_fetch_array($result)) 
    {
      $studentid = $row['student_id'];
      $student_firstname = $row['firstname'];
      $student_lastname = $row['lastname']; 
    }

    if(isset($_GET['student_firstname']))
      {
        extract($_GET);
        $query = "select * from student_exams where exam_name = '$exam_name' and student_id ='$student_id'";
        $result = mysqli_query($link,$query);
        $row = mysqli_fetch_array($result);
        if($row > 0 )
        {
          $_SESSION['saved'] = "false";// Change made HERE, this variable stores your result
          $msg .="<h5 style='text-align:center;color:red;'>Your Exam Detail Already Exist. Please Select Other Exam.</h5>";
        }
        else
        {
            $sql = "insert into student_exams(student_id,student_firstname,student_lastname,exam_name,date)values('$student_id','$student_firstname','$student_lastname','$exam_name','$date')";
            $result=mysqli_query($link,$sql);
            if($result == true)
            {
            $msg .="<h4 style='text-align:center;color:green;'>Your Data Has Been Submitted Successfully.</h4>";
            $_SESSION['saved'] = "true";// Change made HERE, this var stores your result

            }
            else
            {
              $_SESSION['saved'] = "false";// Again, this variable stores your result
              $msg .="<h4 style='text-align:center;color:red;'>Error</h4>";
            }
        }
      }
    ?>

此处我有已使用的会话来存储帖子 是否成功 ,具体取决于我更改了已检查的属性复选框。所以基本上$_SESSION['saved']存储了查询是否成功。如果失败,则此变量包含值false,否则为true。因此,上述代码将帮助您在查询成功时检查输入。