问题
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!
回答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"/>
回答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']) ) {
回答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
回答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.
回答5:
Replace:
if(isset($_POST['checkAccount'])
to:
if(isset($_GET['checkAccount'])
回答6:
// 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');
?>
来源:https://stackoverflow.com/questions/17041268/php-submit-checkbox-to-database