hi i have a checkbox and i want to save its value into DataBase if it checked and save different text if its not checked.
i tried to this code , it works but if i have 2 checkboxe it doesn't work any more.
tried code in view.php :
$ch = SELECT check FROM users;
if($ch=="checked"){
echo "checked";
}else{
echo "not checked";
}
html :
<form action="action.php" method="post" id="form">
<p>checkbox: <input type="checkbox" name="check" value="checked" /></p>
<input type="submit" value="submit" />
</form>
action.php :
<?php
define('DB_NAME', 'users');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect : ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can not use '.DB_NAME . ' : ' . mysql_error());
}
$check =$_POST['check'];
$sql = "INSERT INTO users (check) VALUES ('$check')";
echo "your informations were sent to our database click to come back";
mysql_query($sql);
mysql_close();
?>
view.php :
<?php
$dbname = "users";
$username = "root";
$password = "";
$servername = "localhost";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "checkbox : ". $row["check"];
}
} else {
echo "<a class='btsc'>no one registrated if you want register complete the form</a>";
}
$conn->close();
?>
db :
As has been said, checkbox values are not set when they are not checked.
Here is a little work around I have used:
<input type="hidden" name="check" value="false" />
<input type="checkbox" name="check" id="check" value="true" />
Now if the checkbox is checked then the hidden element with the same name is overwritten.
page1.html
HTML
<form action="page2.php" method="post" id="form">
<p>checkbox: <input type="checkbox" name="check" id="check" value="checked" /></p>
<input type="submit" value="submit" />
</form>
page2.php
PHP
<?php
// Take the value of the checkbox
$varcheck = $_POST['check'];
// Check if the value of $_POST is correct
echo "The value is: $varcheck";
?>
Connect now to your database and run an INSERT INTO QUERY, like this:
INSERT INTO users (check) VALUES ('$varcheck');
$_POST['check']
will be undefined if the user submitted the form with the checkbox unchecked.
So, if you want to store different values depending if it's checked, use something like isset($_POST['check'])
.
if ( isset($_POST['check']) ) {
$check = "some value";
} else {
$check = "another value";
}
$sql = "INSERT INTO users (check) VALUES ('$check')";
I also strongly urge you to use PDO or mysqli to perform all MySQL database operations - noticed you used mysql_query()
which has been deprecated for major security reasons!! (see the big red box at http://php.net/manual/en/function.mysql-query.php )
// this, is the html index.php file
<form action="test.php" method="post" id="form">
<p><input type="checkbox" name="myCheck" data-toggle="toggle" data-
on="absent" data-off="présent" data-onstyle="danger" data-offstyle="secondary"></p>
<input type="submit" value="submit" />
</form>
// Now, we are redirecting... test.php file
<?php
// Take the value of the checkbox
$var = $_POST['myCheck'];
if ($var == 'on') {
$var = 1;
echo "The value is: $var</br>";
}else {$var = 0;
echo "The value is: $var</br>";
}
$sql = "INSERT INTO users (check) VALUES ('$var')";
//..
//...
// value will be 1 or 0;
?>
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.