2

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!

6 Answers 6

2

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"/>
Sign up to request clarification or add additional context in comments.

3 Comments

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?
$ckeckAccount = $_GET['checkAccount']; if ($ckeckAccount != null) {//do something}
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
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']) ) {

Comments

1

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

1 Comment

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

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.

Comments

0

Replace:

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

to:

 if(isset($_GET['checkAccount'])   

Comments

0

// your html code shoul be like this

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

<?php

    $table = $_GET['table_name'];

    $column = $_GET['account'];

    $value = isset($_GET['checkAccount']) ? 1 : 0;

    $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');

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.