在PHP中检查onchange下拉显示复选框

时间:2017-06-06 09:44:03

标签: php jquery mysqli

代码:

<script>
  $(document).ready(function(){
    $(".menu").click(function(){
      ids = $('.menu:checked').map(function() {
              return this.id;
              }).get().join(',');
              console.log(ids);
        $("#ids").val(ids);      
    });
  });
</script>

<?php
if(isset($_POST['submit']))
{
  $adminid = $_POST['admin'];
  $menuids = explode(",", $_POST['ids']);
    foreach ($menuids as $idd) 
    {
      $sql = "update menu set admin_id = concat(admin_id,'$adminid',',') where id = '$idd'";
      $result = mysqli_query($link,$sql);
    }
    if($result == true)
      {
        $msg .= "<p style='color:green'>successfull</p>";
      }
      else
      {
        $msg .= "<p style='color:red'>error!</p>";
      }
}
?>

<form method="post">
  <select name="admin" id="admin">
    <option value="">---Select Admin---</option>
    <?php
      $sql = "select * from admin";
      $result = mysqli_query($link,$sql);
      while ($row = mysqli_fetch_array($result)) 
      {
    ?>
     <option value="<?php echo $row['id']; ?>"><?php echo $row['firstname']?></option> 
    <?php    
      }
    ?>
  </select>
  <table>
    <tr>
      <th>Share</th>
      <th>Menu Name</th>
    </tr>
    <?php
      $query = "select * from menu";
      $results = mysqli_query($link,$query);
      while ($fetch = mysqli_fetch_array($results)) 
      {
    ?>
        <tr>
          <td>
            <input type="checkbox" class="menu" id="<?php echo $fetch['id']; ?>" name="menuid" />
          </td>

          <td>
            <?php echo $fetch['menu_name']; ?>
          </td>
        </tr>
    <?php
      }
    ?>
  </table>
  <input type="text" name="ids" id="ids" value=""/>
  <input type="submit" name="submit" id="submit" />
</form>

在这段代码中,我更新了一个在数据库中有名称菜单的表。现在,我想只检查admin_id like,1或2的复选框,它们是按查询更新的。我该如何解决这个问题?请帮助。

谢谢

1 个答案:

答案 0 :(得分:0)

   while ($fetch = mysqli_fetch_array($results)) 
  {
   ?>
    <tr>
      <td>
        <input type="checkbox" class="menu" value="<?php if($fetch['id']==1 or 
           $fetch['id']==2 ) { echo "checked";} else{} ?>" name="menuid" />
      </td>

      <td>
        <?php echo $fetch['menu_name']; ?>
      </td>
    </tr>
<?php
  }
?>