如何显示html中复选框输入的选定值

时间:2017-10-17 13:06:16

标签: php html mysql

  • 我希望能够从我选中的复选框中获取值
  • 对于第二行等的第一行1,该值将为0
  • 代码工作并显示所有内容但显示所有值为 0意味着它不会增加它。

=============================================== =========================

 <html>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <button type='submit' name='submit' id='buttonParent'>Submit</button>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>

    <script src="http://code.jquery.com/ui/1.9.1/jquery-ui.min.js" type="text/javascript"></script>
    <script>
    $(document).ready(function () {
        /* Get the checkboxes values based on the class attached to each check box */
        $("#buttonClass").click(function() {
            getValueUsingClass();
        });

        /* Get the checkboxes values based on the parent div id */
        $("#buttonParent").click(function() {
            getValueUsingParentTag();
        });
    });

    function getValueUsingParentTag(){
        var chkArray = [];

        /* look for all checkboes that have a parent id called 'checkboxlist' attached to it and check if it was checked */
        $("#checkboxlist input:checked").each(function() {
            chkArray.push($(this).val());
        });

        /* we join the array separated by the comma */
        var selected;
        selected = chkArray.join(',') ;

        /* check if there is selected checkboxes, by default the length is 1 as it contains one single comma */
        if(selected.length > 1){
            alert("You have selected " + selected); 
        }else{
            alert("Please at least one of the checkbox");   
        }
    }
    </script>
    <?php
    session_start();
    require_once('../mysql_connect.php');

                  $query = "select * from inventory";
                  $res = mysqli_query($dbc, $query);

                  echo
                      '<br><br><br><br><br>
                        <div class = "Table">
                          <table border = "2pt solid black" align = "left" cellpadding = "2px"
                                 bordercolor = black>
                          <tr>
                            <td width = 7%">
                              <div align = "left"><b>CHECK.</div></b>
                            </td>
                              <td width = "7%">
                              <div align = "left"><b>INVENTORY NAME</div></b>
                            </td>
                             <td width = "3%">
                              <div align = "left"><b>CATEGORY</div></b>
                            </td>

                            <td width = "3%">
                              <div align = "left"><b>Quantity</div></b>
                            </td>

                            <td width = "7%">
                              <div align = "left"><b>MEASUREMENT</div></b>
                            </td>
                            <td width = "7%">
                              <div align = "left"><b>SPOILAGE DATE</div></b>
                            </td>
                          </tr>';
                           $num_rows = mysqli_num_rows($res);
            for($i=0;$i<$num_rows;$i++)
            {
                  while($fetch = mysqli_fetch_array($res, MYSQL_ASSOC))
                  {



                      echo "<tr>
                                <td width=\"7%\">
                                  <div id='checkboxlist' align=\"left\"><input type='checkbox' class='chk' value='$i'/></div>
                                </td>
                                <td width=\"7%\">
                                  <div align=\"left\">{$fetch['inventory_name']}</div>
                                </td>
                                <td width=\"3%\">
                                  <div align=\"left\">{$fetch['category']}</div>
                                </td>
                                <td width=\"3%\">
                                  <div align=\"left\"><input  type = 'number' placeholder = {$fetch['quantity']} type = 'number' name = 'qty[]' id='qty' class = 'qty'/></div>
                                </td>
                                <td width=\"7%\">
                                  <div align=\"left\"><input type='text' name='measure' class='measure' id='measure'/></div>
                                </td>
                                <td width=\"7%\">
                                  <div align=\"left\">{$fetch['spoilage_date']}</div>
                                </td 
                            </tr>"; 
                      }                     
             }

                  echo '</table></div>';
    ?>
    </form> 



    </html>

1 个答案:

答案 0 :(得分:0)

你在for的内心是原因。你根本不需要for循环。在while之前设置{{1}}并在里面增加它。