我想获得多个复选框的ID

时间:2017-04-02 21:40:38

标签: php html forms

我想要复选框中所选项目的ID并插入数据库。我可以将其他项插入数据库,但是未插入所选复选框的ID。

    <div class="row" >
      <div class="col-xs-3">
      <form action="itemad.php" method="post">

      <div class="multiselect">
      <input type="text" name="datepicker" id="datepicker" placeholder="Date"     </p>
        <b>Select Menu:</b>
          <div class="checkbox" onclick="showCheckboxes()">
            <select>
                  <option>Select an option</option>
           </select>
         <div class="overSelect"></div>
        </div>

     <div id="checkboxes">

        <?php
            include 'connection.php';
           $query="select item from item";
            $result=mysql_query("$query");
             while($row=mysql_fetch_array($result)):; 
              echo $row['item'];
               for($i=0; $i<=count('item'); $i++ ) 
        ?>
      <input type="checkbox" name="check_list[]" value="" ></input><br>
           <?php endwhile;?> 
           </div>
         <input type="text" name="price" Placeholder="Pricee" minlength="1" ><br><br>
         <input type="submit" name="submit">  
      </div> 
     </div>
          <script type="text/javascript">
            var expanded = false;
            function showCheckboxes() {
              var checkboxes = document.getElementById("checkboxes");
              if (!expanded) 
              { 
                checkboxes.style.display = "block";
                expanded = true;
              } 
              else
               {
                checkboxes.style.display = "none";
                expanded = false;
               }
            }
          </script>
  </form>
  </div>
  </div>

itemad.php:

if(isset($_POST['submit']))
{
if(!empty($_POST['check_list'])) 
{
    // Counting number of checked checkboxes.
    $checked_count = count($_POST['check_list']);
    echo "You have selected following ".$checked_count." option(s): <br/>";
    // Loop to store and display values of individual checked checkbox.
    foreach($_POST['check_list'] as $id) 
        {
            echo "<p>".$id ."</p>";
        } }
else
    { echo "<b>Please Select Atleast One Option.</b>"; }
   $price=$_POST['price'];
        $date=$_POST['datepicker'];
        $query= "insert into menu(item,price,date) values('$id','$price','$date')";
       $result=mysql_query($query);
       echo "suucesful";
    }
  

我想在数据库中存储每个项目的ID。但在数据库中,我没有获得所选项目的ID。

2 个答案:

答案 0 :(得分:1)

你错过了一件事:

<input type="checkbox" name="check_list[]" value="" />

check_list数组不应该为空值。你应该给它一个值。

<?php 
  $query="select item_id, item from item";
  $result=mysql_query("$query");
  while($row=mysql_fetch_array($result)):;  
  ?>
 <input type="checkbox" name="check_list[]" value="<?php echo $row['item_id']; ?>" /><br>
 <?php endwhile;?> 

答案 1 :(得分:0)

<div class="row" >
    <div class="col-xs-3">
        <form action="itemad.php" method="post">

            <div class="multiselect">
                <input type="text" name="datepicker" id="datepicker" placeholder="Date" />
                <b>Select Menu:</b>
                <div class="checkbox" onclick="showCheckboxes()">
                    <select>
                        <option>Select an option</option>
                    </select>
                    <div class="overSelect"></div>
                </div>

                <div id="checkboxes">
                    <?php
                    include 'connection.php';
                    $query="select item_id, item from item";
                    $result=mysql_query("$query");
                    while($row=mysql_fetch_array($result)):; 
                        echo $row['item'];
                    ?>
                        <input type="checkbox" name="check_list[]" value="<?php echo $row['item_id']; ?>" /><br>
                    <?php endwhile;?> 
                </div>
                <input type="text" name="price" Placeholder="Price" minlength="1" ><br><br>
                <input type="submit" name="submit">  
            </div> 
            <script type="text/javascript">
            var expanded = false;
            function showCheckboxes() {
              if (!expanded) 
              { 
                $('#checkboxes input:checkbox').each(function () {
                   this.style.display = "block";
                });
                expanded = true;
              } 
              else
               {
                $('#checkboxes input:checkbox').each(function () {
                   this.style.display = "none";
                });
                expanded = false;
               }
            }
            </script>
        </form>
    </div>
</div>
  

itemad.php:

if(isset($_POST['submit']))
{
    if(!empty($_POST['check_list'])) 
    {
        // Counting number of checked checkboxes.
        $checked_count = count($_POST['check_list']);
        echo "You have selected following ".$checked_count." option(s): <br/>";
        // Loop to store and display values of individual checked checkbox.
        foreach($_POST['check_list'] as $id) 
        {
            echo "<p>".$id ."</p>";
            $price=$_POST['price'];
            $date=$_POST['datepicker'];
            $query= "insert into menu(item,price,date) values('$id','$price','$date')";
            $result=mysql_query($query);
            echo "suucesful";
        } 
    }
    else
    { 
        echo "<b>Please Select Atleast One Option.</b>"; 
    }
}