简体   繁体   中英

check if checkbox is checked in php

I have a check box list which I fill it with data from my table.Here is the code:

<?php
mysql_connect("localhost","root","");
mysql_select_db("erp");


$a="Select * from magazine";
$b=mysql_query($a);

$c=mysql_fetch_array($b);

while($c=mysql_fetch_array($b))

     {
            print '<input type="checkbox"/>'.$c['den_mag'];
            echo "</br>";

            }
    if(isset($_POST['den_mag']))

{
      echo "aaaa";

}     
?>

It's a simple query and for each data just show it with a checkbox.Now what I want is when I press a checkbox the value of that checkbox to be shown in a table.So if I have check1 with value a , check2 with value b and I check check1 the value a to be outputted to a table row.How can I achieve that? how cand I get which checkbox is checked?

<input name="test" type="checkbox" />


<?php

    if(isset($_REQUEST['test'])){
        // selected
    }

?>

When you give input-type elements (input, textarea, select, button) a name attribute (like I did), the browser will submit the state/value of the element to the server (if the containing form has been submitted).


In case of checkboxes, you don't really need to check the value, but just that it exists. If the checkbox is not selected, it won't be set.


Also, you need to understand the client-server flow. PHP can't check for something if the client does not send it.

And finally, someone mentioned jQuery. jQuery is plain javascript with perhaps some added sugar. But the point is, you could in theory change stuff with jQuery so that it gets (or doesn't get) submitted with the request. For example, you could get jQuery to destroy the checkbox before the form is submitted (the checkbox won't be sent in this case).

A few notes:

  • Try to avoid using SELECT * queries. Select the fields you are going to use:

    $sql= ' SELECT id , den_mag FROM magazine ';

  • Use better variable names. $a and $c make your code harder to follow for others, and for yourself when you come back at a later time. Use more descriptive variable names like $query_object and $row . Your code should read almost like an essay describing what you're doing.

  • In your form, use an array of elements. By giving the input a name like selected_magazines[] , you will end up with an array in your post data, which is what you want -- multiple selections

  • Use the row ID as the value of the checkbox element. Your array in POST will then be a list of all the IDs that the user selected

  • Separate your logic from your HTML generation. The top portion of your script should take care of all logic and decisions. At the bottom, output your HTML and avoid making logical decisions. It makes for a script that is easier to follow and maintain, as well as debug.

Here is a sample script incorporating these ideas with the details you've given:

<?php
    // FILE: myfile.php

    mysql_connect("localhost","root","");
    mysql_select_db("erp");

    if(isset($_POST['selected_magazine'])) {

        // $_POST['selected_magazine'] will contain selected IDs

        print 'You selected: ';
        print '<ul><li>'.implode($_POST['selected_magazine'], '</li><li>').'</li></ul>';
        die();
    }

    $sql= '
        SELECT 
            `id`,
            `den_mag` 
        FROM
            `magazine`
    ';
    $query_object=mysql_query($sql);

    $checkboxes = array();
    while($row = mysql_fetch_array($query_object)) {
        $checkboxes[] = '<input name="selected_magazine[]" value="'.$row['id'].'" type="checkbox" /> '.$row['den_mag'];
    }
?>
<form action="myfile.php" method="post">
    <?php print implode('<br>', $checkboxes); ?>
    <input type="submit" value="Submit" />
</form>

Here you go :

<html>
    <input name="test" value="true" type="checkbox" />
</html>

<?php
$Checkbox1 = "{$_POST['test']}";

if($Checkbox1 == 'true'){
    // yes, it is checked
}
?>

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