【问题标题】:onchange dropdown show checkbox is checked in php在 php 中选中 onchange 下拉显示复选框
【发布时间】:2017-06-06 09:44:03
【问题描述】:

代码:

<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 像 ,1 或 ,2 的复选框,这些复选框是通过查询更新的。我该如何解决这个问题?请帮忙。

谢谢

【问题讨论】:

    标签: php jquery mysqli


    【解决方案1】:
       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
      }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-19
      • 2021-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-30
      • 2021-04-14
      • 1970-01-01
      相关资源
      最近更新 更多