简体   繁体   中英

Best way to use checkboxes with PHP

first off I am a rookie so please forgive my basic question. I am trying to use an html checkbox to let me know when to check a serial number. The problem is no matter what i do I get an error telling me "Notice: Undefined index: sncheck in XYZ".

I tried using a ternary operator to set the value as it was posted (If the checkbox was not checked) but it still fails on me. Any suggestions would be

Here is the code, below im using a checkbox with the name sncheck and a value of ok. thanks!

if (isset($_POST['submit'])){
    $gov = mysqli_real_escape_string( $dbc, trim($_POST['gov']));
    $pctype = mysqli_real_escape_string( $dbc, trim($_POST['pctype']));
    $compname = mysqli_real_escape_string( $dbc, trim($_POST['compname']));
    $username = mysqli_real_escape_string( $dbc, trim($_POST['username']));
    $compmodel = mysqli_real_escape_string( $dbc, trim($_POST['compmodel']));
    $serialnumber = mysqli_real_escape_string( $dbc, trim($_POST['serialnumber']));
    $sncheck = ($_POST['sncheck']== 'ok') ? $_POST['sncheck'] : '';
    $purchdate = mysqli_real_escape_string( $dbc, trim($_POST['purchasedate']));
    $os = mysqli_real_escape_string( $dbc, trim($_POST['os']));
    $memory = mysqli_real_escape_string( $dbc, trim($_POST['memory']));
    $monitor1 = mysqli_real_escape_string( $dbc, trim($_POST['monitor1']));
    $monitor2 = mysqli_real_escape_string( $dbc, trim($_POST['monitor2']));
    $warranty = mysqli_real_escape_string( $dbc, trim($_POST['warranty']));
    $warrantyend = mysqli_real_escape_string( $dbc, trim($_POST['warrantyend']));
    $status = mysqli_real_escape_string( $dbc, trim($_POST['status']));
    $notes = mysqli_real_escape_string( $dbc, trim($_POST['notes']));

    if (dateformat($purchdate) == 1 && $sncheck == "ok"){
            $querysn = "SELECT serialnumber FROM inventory" . 
                                " WHERE serialnumber = '" . $serialnumber . "'";
            $data = mysqli_query($dbc, $querysn) or die('Error querying the Database for Serial Numbers');
            if (mysqli_num_rows($data) == 0){
                $queryupdate = "UPDATE inventory SET government = '$gov', pctype = '$pctype', compname = '$compname'," . 
                                    " username = '$username', compmodel = '$compmodel', serialnumber = '$serialnumber', purchased = '$purchdate'," . 
                                    " operatingsystem = '$os', `memory` = '$memory', monitor1 = '$monitor1'," . 
                                    " monitor2 = '$monitor2', warranty = '$warranty', warrantyend = '$warrantyend', `status` = '$status', notes = '$notes'" . 
                                    " WHERE serialnumber = '" . $_POST['serialnumber'] . "'";
                mysqli_query($dbc, $queryupdate);
                echo 'PC was sucessfully saved';
            }
            else{
                echo '<p class="error">The serial number has already been used, please try another</p>';
            }
    }
    else{
        echo '<p class="error">The date format does not match</p>';
    }

    if (dateformat($purchdate) == 1){
            $querysn = "SELECT serialnumber FROM inventory" . 
                                " WHERE serialnumber = '" . $serialnumber . "'";
            $data = mysqli_query($dbc, $querysn) or die('Error querying the Database for Serial Numbers');
            if (mysqli_num_rows($data) == 0){
                $queryupdate = "UPDATE inventory SET government = '$gov', pctype = '$pctype', compname = '$compname'," . 
                                    " username = '$username', compmodel = '$compmodel', purchased = '$purchdate'," . 
                                    " operatingsystem = '$os', `memory` = '$memory', monitor1 = '$monitor1'," . 
                                    " monitor2 = '$monitor2', warranty = '$warranty', warrantyend = '$warrantyend', `status` = '$status', notes = '$notes'" . 
                                    " WHERE serialnumber = '" . $_POST['serialnumber'] . "'";
                mysqli_query($dbc, $queryupdate);
                echo 'PC was sucessfully saved';
            }
            else{
                echo '<p class="error">The serial number has already been used, please try another</p>';
            }
    }
    else{
        echo '<p class="error">The date format does not match</p>';
    }
}

EDITED ANSWER: Based on your edited question, try changing:

$sncheck = ($_POST['sncheck']== 'ok') ? $_POST['sncheck'] : '';

to:

$sncheck = isset($_POST['sncheck']);

If a checkbox is checked, it will be set in $_POST . If it's not checked it will not be.

ORIGINAL ANSWER:

It's tough to answer your question without seeing code, so I'm just stabbing in the dark, but here goes. Here's an example:

<?php

$serial_numbers = array('1','2','3'); // Put your real serial numbers here...

if(isset($_REQUEST['submit']))
{
    if(isset($_REQUEST['serial_numbers']) && is_array($_REQUEST['serial_numbers']))
    {
        foreach($_REQUEST['serial_numbers'] as $serial)
        {
            // Do something with this serial number
            echo "You checked $serial<br>";
        }
    }
}
else
{
    // Show a form to select serial numbers:

    echo "<form action=\"$PHP_SELF\">";
    foreach($serial_numbers as $serial)
    {
        $serial_ht = htmlspecialchars($serial);
        echo "<input type=\"checkbox\" name=\"serial_numbers[]\" value=\"$serial_ht\">$serial_ht<br>";
    }

    echo '<input type="submit" name="submit" value="Submit">';
}

This is a complete guess because I'm not sure what you're doing. But this will take an array of serial numbers and display a form containing them all with checkboxes next to each one. When the form is submitted, the selected serial numbers will be in $_REQUEST['serial_numbers'] (which will be an array). The script will loop through each member of this array and print those serial numbers back. Not very useful, but again, it's just an example. Please post some code if you'd like more direction and I'll edit my answer.

Use isset($_GET['id']); or isset($_POST['id']) to check if a field - such as a checkbox - was submitted in a form.

(Note however, that textfields and some other form inputs will be "" and will therefore be considered as "submitted" using the method above)

EDIT:
Also, you didn't get an error, but a notice . Notices doesn't mean that something is wrong, but that you for instance should have done something instead of what you actually did etc. for the PHP script to be better. Warnings , however, are "non-fatal errors", and errors are simply fatal errors that will cause the script to halt execution.

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