复选框和同一行的输入值

时间:2016-11-22 02:21:49

标签: javascript php html mysql

我创建了一个显示药物列表的表格。它有库存处理功能。

问题是,如果我有3条记录的数据,我只能检查记录1号和2号,如果我检查记录号3(或者如果我记录了这么多记录的最后一条记录)我收到错误。

问题是,如果我检查数据3(或最后一条记录),复选框将获得serialno,但不会获得数量值。

当我注册一种新药时也会出现问题,然后我检查该框以插入该值并返回:

  

插入检查医学的医学数量。

如何解决此问题?我尽可能地减少了代码。

表格:

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" class="form-horizontal" role="form">  
    <div class="form-group">
        <label class="col-sm-3 control-label no-padding-right" for="form-name"> Search : </label>

        <div class="col-sm-9">
            <span class="input-icon">
                <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Keyword HERE" class="col-xs-10 col-sm-12" name="search" />
                <i class="ace-icon fa fa-search green"></i>
            </span>
        </div>
    </div>
    <table id="myTable" class="table  table-bordered table-hover">
        <thead>
            <tr>
                <th>Name</th><th>Serial Number</th><th>Price</th><th>Current Stock</th><th>Action</th><th>Value</th>
            </tr>
        </thead>
        <tbody>                                     
        <?php
            $sqlTable = "SELECT * FROM medicinestock";
            $queryTable = $conn -> query($sqlTable);

            if ($queryTable -> num_rows > 0)
            {
                while ($resultTable = $queryTable -> fetch_assoc())
                {
                    $price = number_format($resultTable['price'], 2);
                    ?>
                    <tr>
                        <td><?php echo $resultTable['name']; ?></td>
                        <td><?php echo $resultTable['serialNo']; ?></td>
                        <td>RM <?php echo $price; ?></td>
                        <td><?php echo $resultTable['quantity']; ?></td>
                        <td>
                            <div class="checkbox">
                                <label>
                                    <input name="checkboxCheck[]" type="checkbox" class="ace" value="<?php echo $resultTable['serialNo']; ?>"/>
                                    <span class="lbl">&nbsp;&nbsp;Check</span>
                                </label>
                            </div>
                        </td>
                        <td>
                            <input type="number" name="quantity[]" style="max-width:50px" />
                        </td>
                    </tr>
                    <?php
                }
            }
            else
            {
                $err = $conn -> error;
                $status = "<b><p><font color='red'>SYSTEM ERROR : $err</font></p></b>";
            }
        ?>
        </tbody>
    </table>
    <div class='clearfix form-actions'>
        <div class='col-md-offset-3 col-md-9'>
            <button class='btn btn-info' type='submit'>
                <i class='ace-icon fa fa-check bigger-110'></i>
                Finish
            </button>
        </div>
    </div>
</form>

过程

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
    $icnumber = $_POST['icnumber'];

    //Check if CHECKBOX checked
    if (isset($_POST['checkboxCheck']))
    {
        $quantity = $_POST['quantity'];
        $checkboxCheck = $_POST['checkboxCheck'];
        $count = count($checkboxCheck);
    }

    if ($count == 0)
    {
        $status = "<font color='red'><p><b>PLEASE CHECK AT LEAST 1 MEDICINE PRESCRIPTION</b></p></font>";
    }
    else
    {   
        //Run process for each checked checkbox
        for ($i = 0; $i < $count; $i++)
        {
            $serialNo = $checkboxCheck[$i];
            $value = $quantity[$i];

            if ($value == null)
            {
                $checker = FALSE;
            }
            else
            {
                $sqlPrescription = "INSERT INTO medicinecheckout (patientic, serialno, quantity, medicineout) VALUES ('$icnumber', '$serialNo', $value, NOW())";
                $queryPrescription = $conn -> query($sqlPrescription);

                if ($queryPrescription == FALSE)
                {
                    $err = $conn -> error;
                    $status = "<b><p><font color='red'>SYSTEM ERROR : $err</font></p></b>";
                }
            }
        }
    }

    if ($checker == FALSE)
    {
        $status = "<b><p><font color='red'>INSERT MEDICINE QUANTITY FOR CHECKED MEDICINE</font></p></b>";
    }
}

<script type="text/javascript">
    function myFunction() 
    {
        var input, filter, table, tr, td, i;
        input = document.getElementById("myInput");
        filter = input.value.toUpperCase();
        table = document.getElementById("myTable");
        tr = table.getElementsByTagName("tr");

        // Loop through all table rows, and hide those who don't match the search query
        for (i = 0; i < tr.length; i++) 
        {
            td = tr[i].getElementsByTagName("td")[0];
            if (td) 
            {
                if (td.innerHTML.toUpperCase().indexOf(filter) > -1) 
                {
                    tr[i].style.display = "";
                } 
                else 
                {
                    tr[i].style.display = "none";
                }
            } 
        }
    }
</script>

2 个答案:

答案 0 :(得分:0)

所以基本上我改变了#34;输入数字名称&#34;作为序列号,然后我$ _POST并将序列号作为索引。

<?php
    //Run process for each checked checkbox
    for ($i = 0; $i < $count; $i++)
    {
        $serialNo = $checkboxCheck[$i];

        //Change HERE
        $value = $_POST[$serialNo];

        if ($value == null)
        {
            $checker = FALSE;
        }
        else
        {
            $sqlPrescription = "INSERT INTO medicinecheckout (patientic, serialno, quantity, medicineout) VALUES ('$icnumber', '$serialNo', $value, NOW())";
            $queryPrescription = $conn -> query($sqlPrescription);

            if ($queryPrescription == FALSE)
            {
                $err = $conn -> error;
                $status = "<b><p><font color='red'>SYSTEM ERROR : $err</font></p></b>";
            }
        }
    }
    ?>

    <!-- FORM -->
    <tr>
        <td><?php echo $resultTable['name']; ?></td>
        <td><?php echo $resultTable['serialNo']; ?></td>
        <td>RM <?php echo $price; ?></td>
        <td><?php echo $resultTable['quantity']; ?></td>
        <td>
            <div class="checkbox">
                <label>
                    <input name="checkboxCheck[]" type="checkbox" class="ace" value="<?php echo $resultTable['serialNo']; ?>"/>
                    <span class="lbl">&nbsp;&nbsp;Check</span>
                </label>
            </div>
        </td>
        <td>
        <!-- and here, change the name -->
            <input type="number" name="<?php echo $resultTable['serialNo']; ?>" style="max-width:50px" />
        </td>
    </tr>

答案 1 :(得分:0)

复选框仅在选中时返回$ _POST值。基于复选框数组的计数循环将不起作用。获取返回值的方法是在复选框之前包含一个隐藏了相同名称的输入类型,例如value =“0”。

<input type="hidden" name="checkboxCheck" value="0" />
<input type="checkbox" name="checkboxCheck" />

现在可以使用:

$checkboxChecks = $_POST['checkboxCheck'];
$quantity = $_POST['quanty'];
for($i =0;$i<count($quantity);$i++){
    $checked = $checkboxChecks[$i];
    $qty = $quantity[$i];
    ...
}