无法获取复选框选中的值来更新数据库

时间:2017-07-17 12:49:40

标签: php jquery mysql checkbox admin

我已经搜索了很多以找到这个问题的解决方案,但还没有成功。我正处于一个项目,管理员必须批准从数据库列出的注册用户。基本上我的问题是如何在提交时使所选用户(来自复选框),在数据库字段中设置1'状态'。我无法弄清楚如何处理代码。

我是网络开发的新手。在这里,我发布了我正在工作的页面的代码:

<?php include 'blocks/headerInc.php' ; ?>
<?php require_once "phpmailer/class.phpmailer.php";?>
<?php
$errmsg         =   "" ;
$module_id      =   '';
$query          =   '';
$date_from      =   '';
$date_to        =   '';
$status ='';

$sqlQuery       =   "SELECT * FROM tbl_user WHERE type =3 " ;

if(isset($_GET['users'])){
$users=$_GET['users'];
}else{
    $users='';
}
switch ($users)
{
case "all":
  $sqlQuery = "SELECT * FROM tbl_user WHERE type =3";
break;
case "approved":
   $sqlQuery = "SELECT * FROM tbl_user WHERE type =3 AND status =1";
break;
case "unapproved":
  $sqlQuery = "SELECT * FROM tbl_user WHERE type =3 AND status =0";
break;
}
?>
<div class="container pagecontainer">
  <div class="row row-offcanvas row-offcanvas-right">
 <div class="col-sm-3 col-md-3 sidebar" id="sidebar">
 <div id="left_panel" class="clearfix left">
 <?php include 'blocks/leftnavInc.php' ; ?>
</div>
    </div>
    <div class="col-xs-12 col-sm-9 page-right">
        <div class="panel panel-primary">
        <div class="panel-heading">Search Registered Candidate</div>
        <div class="panel-body">
          <div class="column col-sm-offset-0">
                <?php
                if($errmsg!="")
                {
                    echo "<div class='error'>".ucwords($errmsg)."</div>";
                }
                ?>
       <form class="form-horizontal" method="get" action="">
              <div class="form-group">
                <div class="col-md-6">
                  <div class="col-md-4">
                    <label for="username" class="control-label">Date From:</label>
                  </div>
                  <div class="col-md-8">
                  <div class="input-group date">
                     <input class="form-control datepicker" data-val="true" data-val-date="The field Dob must be a date." data-val-required="The Dob field is required." id="Dob" name="date_from" placeholder="Date From" type="text" value="<?php echo $date_from ; ?>" >
                      </div>
                  </div>
                </div>
                <div class="col-md-6">
                  <div class="col-md-4">
                    <label for="username" class="control-label">Date To:</label>
                  </div>
                  <div class="col-md-8">
                    <div class="input-group date">
                      <input class="form-control datepicker" data-val="true" data-val-date="The field Dob must be a date." data-val-required="The Dob field is required." id="Dob" name="date_to" placeholder="Date To" type="text" value="<?php echo $date_to ; ?>" >
                     </div>
                  </div>
                </div>
              </div>
              <div class="form-group">
                <div class="col-md-6">
                  <div class="col-md-8 text-left">
                    <button type="submit" name="submit" value="submit" class="btn btn-success"><i class="glyphicon glyphicon-floppy-disk"></i> Search</button>
                   <button type="reset" onClick="javascript:window.location.href='reportRegisteredUsers.php'" class="btn btn-danger"><i class="glyphicon glyphicon-ban-circle"></i> Cancel</button>
                  </div>
               </div>
                <div class="col-md-6">
                  <div class="col-md-4">
                    <label for="username" class="control-label"> </label>
                  </div>
                  <div class="col-md-8 text-right">
                  </div>
                </div>
               </div>
              </div>
   </div>
      </div> 
        <div class="panel panel-primary"> 
        <div class="panel-heading">Report:Approved & Unapproved Candidates</div>
 <div class="panel-body">
      <form class="form-horizontal" action="" method="GET">
          <input type='radio' id='hide' name='users' value='all' <?php if (isset($_GET['users']) && $_GET['users'] == 'all')  echo ' checked="checked"';?> checked /> All Candidates<br> 
          <input type='radio' id='hide' name='users' value='approved' <?php if (isset($_GET['users']) && $_GET['users'] == 'approved') echo ' checked="checked"';?> / > Approved Candidates<br>
          <input type='radio' name='users' value='unapproved'  <?php if (isset($_GET['users']) && $_GET['users'] == 'unapproved')  echo ' checked="checked"';?> /> Unapproved Candidates<br><br> 
         <input type="submit" value="submit" ><br><br>
          <div class="column col-sm-offset-0">
            <table id="example" class="table table-striped table-hover table-bordered  dt-responsive nowrap"   cellspacing="0" width="100%">
                            <thead>
<tr>
                      <th>S.No.</th>
                      <th>Email ID</th>
                      <th>SBI Employee ID</th>
                     <th>Name</th>
                     <th>Mobile No.</th>
                     <th>Date of Birth</th>
                    <th>Registration Date</th>
                 <th>Check for Approval <input type="checkbox" id="select_all"/> </th>
                   </tr>
 </thead>
              <tbody>

<?php
$sq             =   $db->query($sqlQuery);
$i              =   1 ;
if($db->affected_rows > 0)
{
    while($row=mysql_fetch_array($sq))
    {
        extract($row);
    $disabled = '';  // default
if ($status == '1') { 
    $disabled = "disabled = 'disabled' checked='checked' "; 
 }
?>              
                <tr>

                  <td><?php echo $i ; ?></td>
                  <td><?php echo $email ; ?></td>
                  <td><?php echo $employee_id ; ?></td>
                  <td><?php echo $first_name."&nbsp;".$middle_name."&nbsp;".$last_name ; ?></td>
                  <td><?php echo $mobile ; ?></td>
                  <td><?php if($dob !='1970-01-01'){echo date("d-m-Y", strtotime($dob)) ; }?></td>
                  <td><?php echo date("d-m-Y", strtotime($created_on)) ; ?></td>
                  <td><input type="checkbox" name="all_check[]" <?php echo $disabled ;?> value="<?php echo $row['id']; ?>" class="checkbox" id="status" ></td>

                </tr>
<?php $i++;}} ?>              
               </tbody>
            </table>
                     <button name = "update" style="display: none"  id="one" class="btn btn-success">Approve Candidate</button>
                </form> 
                </div>
</div>
      </div>
    </div>
  </div>
 </div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(function(){
    $('[type=checkbox]').click(function ()
    {
        var checkedChbx = $('[type=checkbox]:checked');
        if (checkedChbx.length > 0)
        {
            $('#one').show();
        }
        else
        {
            $('#one').hide();
        }

        if (checkedChbx.length == $('[type=checkbox]').length)
        {
            $('#status').show();
        }
        else
        {
            $('#all').hide();
        }
    });
});
</script>
     <script>
    $(document).ready(function(){
               $(document).on('click',".wrp_checkbox_class",function(){
          //alert('hi');
          var closest_form = $(this).closest('form');
          var closest_tr = $(this).closest('tr');
         var data = {
                               'upload_status' :closest_tr.find("input[name='upload_status']").val(),
                               // 'password' :closest_tr.find("input[name='password']").val(),
                                'user_id' :closest_tr.find("input[name='user_id']").val()
                                                          };
                           console.log(data);
                  $.ajax({
                  type: 'POST',
                  url :'registration_status.php',
                  data: data,
                  success: function (res) {
                    // alert('form was submitted');
                                                if(res=='success')
                                                {

                                                  alert('form was submitted');
                                                }
                                          }
                      });
              });
                      });

</script>
<script>
        $(document).on("change", "input[name='upload_status']", function () {
            var checkbox = $(this);
            var checked = checkbox.prop('checked');
            var data = {
                               'upload_status' :closest_form.find("#upload_status").val(),
                               'password' :closest_form.find("#password").val(),
                               'user_id' :closest_form.find("#user_id").val()
                                                          };
            $.ajax({
                url:"registration_status.php",
                type: 'post',
                data: data,
                success: function(data) {
                    alert(data);
                },
                error: function(data) {
                   // alert(data);
                    // Revert
                    checkbox.attr('checked', !checked);
                }
            });
        });
    </script> 
<script type="text/javascript">
      var select_all = document.getElementById("select_all"); //select all checkbox
var checkboxes = document.getElementsByClassName("checkbox"); //checkbox items

select_all.addEventListener("change", function(e){
    for (i = 0; i < checkboxes.length; i++) { 
        checkboxes[i].checked = select_all.checked;
    }
});
for (var i = 0; i < checkboxes.length; i++) {
    checkboxes[i].addEventListener('change', function(e){ //".checkbox" change 
        if(this.checked == false){
            select_all.checked = false;
        }

        if(document.querySelectorAll('.checkbox:checked').length == checkboxes.length){
            select_all.checked = true;
        }
    });
}
</script>
<?php include 'blocks/footerInc.php'; ?>

0 个答案:

没有答案