简体   繁体   中英

How to retrieve the checked state of a checkbox from a Html form and store it in a database?

i have a form field

<input type="checkbox" name="page" value=""/>

and corresponding field in mysql db is true and false, if someone click the checkbox i would like to send TRUE value to db via POST, how do i achieve it?

You give the input any value you like:

<input type="checkbox" name="page" value="true"/>

Then, if the checkbox is checked, it will be a successful control and submitted.

<?php
if (isset($_POST['page']) && $_POST['page'] == 'true') {
    // Then insert something into the database as normal
}
?>

If you want to set it when the checkbox is not ticked, then you will need an else to go with the if .

The Form:

<form action="/path/to/processing_script.php" method="POST">
<!--... Other Form Elements go here -->
<input type="text" name="color" />
<input type="checkbox" name="page" value="True"/>
<input type="submit" value="Send to Database" />
</form>

Processing script: (processing_script.php)

<?php

// Check is 'True'?
if ($_POST['page'] != 'True')
{
    $_POST['page'] = 'False';
}

$con=mysqli_connect("your-db-loc","your-db-username","your-db-pass","db-name");

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL Database: " . mysqli_connect_error();
}

mysqli_query($con,"INSERT INTO TableNameHere (True-false, Favorite Color)
VALUES ($_POST['page'], $_POST['color'])");

mysqli_close($con);
?>

This example I created add's 2 data types to a new row on the database.

Replace the text 'TableNameHere' with your db table name, should be in similar format to: 'prefix_colors_tble'

Replace the text "your-db-loc" with your databases location, usually 'localhost' for internal server, or could be a URL for live server.

Replace the text "your-db-username" the username used to login to mysql, must have sufficient privileges.

Replace the text "your-db-pass" the user's password

Replace the text "db-name" database name (not deemed important unless containing multiple databases)

Replace the text "True-false" & "Favourite Color" with your db table column headings as appropriate.

Good Luck. -ps, I know this answer is a few years late. but I hope it can help somebody else. Send your appreciation to http://amazewebs.com/testimonial Thanks.

For a checkbox, the value attribute determines what the value will be if the item is checked. If it isn't checked, then no value will be submitted at all. You should therefore always specify the value attribute on a checkbox.

If you want the checkbox to default to checked, then you also need to specify the checked attribute.

<input type="checkbox" name="page" value="1" checked='checked' />

Collect the follows by reading $_POST after form submission, then write them to the database using mysql_query

the first thing you will have to do is to modify the current html code for checkbox and add something in the value field.

let say you set the value to 1.

The second thing is if you are posting the form, then you will have to process the form using a server side language like PHP,Perl, Java etc.

for eg in PHP you can get the catch what is sent for the page field using $_POST['page'].

Now you will have to do a bit of server side processing to see if the $_POST['page'] == '1', then set $page = 'true'; else set $page = 'false';

the you can insert the $page into the database by using the library functions of the language you are using.

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