简体   繁体   中英

Checkbox and input value at same row

I create a table that show the list of the medicine. And it got stock process function.

The problem is, if I have 3 record of data, I can only check record no 1 and no 2, if I check record no 3 (or the last record if i got so many record) I got error.

The thing is, if i check data 3 (or last record), the checkbox get the serialno , but do not get the quantity value.

The also a problem occurs when I register a new medicine, then I check that box to insert the value and it returns:

INSERT MEDICINE QUANTITY FOR CHECKED MEDICINE.

How do I fix this problem? I have minimized the code as much I could.

The form:

<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>

The process

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>

So basically I change the "input number name" as the serial number, then I $_POST and make the serial number as the index.

<?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>

The checkbox only returns a $_POST value when it is checked. Looping based on the count of the checkbox array won't work. The way to get a return value is to include an input type hidden with the same name before the checkbox with say a value="0".

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

This would now work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM