PHP: Submit checkbox to database

20,977

Solution 1

The classic way to submit data is to add the value attribute to your checkboxes element in your form. On server side you have to ckeck the value for "null".

<input type="checkbox" name="checkAccount" value="putyourvaluehere"/>

Solution 2

Your Html is not ok

It should be

<form method="get" action="check.php">
            <input type="checkbox" name="checkAccount"/>
            <input type="hidden" name="table_name" value="masterChart" />
            <input type="hidden" name="column_name" value="account" />
            <p><input type="submit" class="btn btn-primary" value="Submit" /></p>
    </form>

Also
if(isset($_POST['checkAccount']) {

Should Be
if( isset($_POST['checkAccount']) ) {

Solution 3

Checkbox value will be submitted only when it's checked. Use isset($_GET['checkAccount']) for this:

$var= isset($_GET['checkAccount']) ? 1 : 0; // Or whatever values you use in DB

Solution 4

Try this:

First you have to edit your html code as below;

<form method="get" action="check.php">
    <input type="checkbox" name="checkAccount" value='cool'/>
    <input type="hidden" name="table_name" value="masterChart" />
    <input type="hidden" name="column_name" value="account" />
    <p><input type="submit" class="btn btn-primary" value="Submit" /></p>
</form>

you are not giving value to check box and using submit button inside a tag, it's not good practice.

Share:
20,977
designtocode
Author by

designtocode

I'm a hybrid designer/developer. It has to be both. Also frontend and backend code interest me. { How can you not love it }

Updated on June 12, 2020

Comments

  • designtocode
    designtocode almost 4 years

    I am having trouble submitted checkbox values and details to my database.

    This is my HTML:

    <form method="get" action="check.php">
                <input type="checkbox" name="checkAccount"/>
                <input type="hidden" name="table_name" value="masterChart" />
                <input type="hidden" name="column_name" value="account" />
                <p><a href='check.php'><input type="submit" class="btn btn-primary" value="Submit" /></a></p>
        </form>
    

    This is the check.php:

    $table = $_GET['table_name'];
    $column = $_GET['account'];
    
    $dbc = mysqli_connect('localhost', 'root', 'root', 'database') or die('Connection error!');
    
    if ($value = 1) {
     $checkbox = "INSERT INTO login_table_display(`user`, `table`, `column`, `value`) VALUES(`:user`, '$table', '$column', `$value`)";
     mysqli_query($dbc, $checkbox) or die('Database error, check!');
     }
    
    header('location:index.php');
    

    As you can see above, I used variables to get other details for that checkbox to insert into the table as well.

    After I press submit if the checkbox is checked, this is what's seen in the url:

    http://localhost/admin/check.php?checkAccount=on&table_name=masterChart&column_name=account
    

    Any suggestions or help will be appreciated!

  • designtocode
    designtocode almost 11 years
    I'm not too sure how to add it.. So I take out the if statement and put the $var = isset($_GET['checkAccount']) ? 1: 0; and then add the mySql Insert?
  • designtocode
    designtocode almost 11 years
    Ok I get that, so if I put value="1" it will do the insert? and if the checkbox is unchecked, it will automatically do the else statement?
  • Reporter
    Reporter almost 11 years
    $ckeckAccount = $_GET['checkAccount']; if ($ckeckAccount != null) {//do something}
  • designtocode
    designtocode almost 11 years
    It's working thanks, but I think there's something with my Insert statement, can you please take a look? I'll add it to the main post