How do I see which checkbox is checked? - php

How do I check in PHP whether a checkbox is checked or not?

If the checkbox is checked, then the checkbox's value will be passed. Otherwise, the field is not passed in the HTTP post.
if (isset($_POST['mycheckbox'])) {
echo "checked!";
}

you can check that by either isset() or empty() (its check explicit isset) weather check box is checked or not
for example
<input type='checkbox' name='Mary' value='2' id='checkbox' />
here you can check by
if (isset($_POST['Mary'])) {
echo "checked!";
}
or
if (!empty($_POST['Mary'])) {
echo "checked!";
}
the above will check only one if you want to do for many than you can make an array instead writing separate for all checkbox try like
<input type="checkbox" name="formDoor[]" value="A" />Acorn Building<br />
<input type="checkbox" name="formDoor[]" value="B" />Brown Hall<br />
<input type="checkbox" name="formDoor[]" value="C" />Carnegie Complex<br />
php
$aDoor = $_POST['formDoor'];
if(empty($aDoor))
{
echo("You didn't select any buildings.");
}
else
{
$N = count($aDoor);
echo("You selected $N door(s): ");
for($i=0; $i < $N; $i++)
{
echo htmlspecialchars($aDoor[$i] ). " ";
}
}

Try this
index.html
<form action="form.php" method="post">
Do you like stackoverflow?
<input type="checkbox" name="like" value="Yes" />
<input type="submit" name="formSubmit" value="Submit" />
</form>
form.php
<html>
<head>
</head>
<body>
<?php
if(isset($_POST['like']))
{
echo "<h1>You like Stackoverflow.<h1>";
}
else
{
echo "<h1>You don't like Stackoverflow.</h1>";
}
?>
</body>
</html>
Or this
<?php
if(isset($_POST['like'])) &&
$_POST['like'] == 'Yes')
{
echo "You like Stackoverflow.";
}
else
{
echo "You don't like Stackoverflow.";
}
?>

If you don't know which checkboxes your page has (ex: if you are creating them dynamically) you can simply put a hidden field with the same name and 0 value right above the checkbox.
<input type="hidden" name="foo" value="0" />
<input type="checkbox" name="foo" value="1">
This way you will get 1 or 0 based on whether the checkbox is selected or not.

I love short hands so:
$isChecked = isset($_POST['myCheckbox']) ? "yes" : "no";

Related

How can I save the checkbox values on all wordpress pages for the current user?

I'm new to wordpress and I have a question
How can I save the checkbox values on all wordpress pages for the current user? For all other users, the code does not apply.
I have a code
<form method="POST">
Option One: <input type="checkbox" name="check1" value="1" <?php
if(isset($_POST['check1'])) echo 'checked="checked"'; ?> />
Option Two: <input type="checkbox" name="check2" value="2" <?php
if(isset($_POST['check2'])) echo 'checked="checked"'; ?> />
<br>
<input type="submit" value="Submit" />
</form>
`<?php
if(isset($_POST['check1'])){
echo '22222';
}
elseif(isset($_POST['check2'])){
echo '3455';
}
?>`
but when you go to another page, everything is reset
how do i fix it?
Tell me please
You might work with php session or javascript localstorage.
Working with tabs will be a right solution, or else, storage right here
session_start();
if(isset($_POST['check'])){
$_SESSION['customer_values']['check_1'] = "VALUE1";
}
if(isset($_POST['check2'])){
$_SESSION['customer_values']['check_2'] = "VALUE2";
}
I do not know why but the code in the comments is buggy
Therefore, I will write it to him here
<form method="POST">
Option One: <input type="checkbox" name="check1" value="1" <?php if ( isset($_SESSION
['customer_values'] ['check1'] ) ) echo 'checked = "checked" '; ?> />
Option Two: <input type="checkbox" name="check2" value="1" <?php if ( isset
($_SESSION ['customer_values'] ['check2'] ) ) echo 'checked = "checked" '; ?> />
<br>
<input type="submit" value="Submit" />
</form>
<?php
session_start();
if(isset($_POST['check1'])){
$_SESSION['customer_values']['check1'] = "VALUE1";
}
if(isset($_POST['check2'])){
$_SESSION['customer_values']['check2'] = "VALUE2";
}
?>
You had this code, if yes then for some reason it does not work

compare checkbox value on submit and display message using php

I want to display the message when user clicks on Others checkbox. There are other checkboxes too so I want to compare checkbox value and if Others checkbox is selected then I want to display the message. This is my code:
<input type="checkbox" name="subject[]" value="Subject-1+Subject-2">Subject-1+Subject-2
<input type="checkbox" name="subject[]" value="Subject-1+Subject-2+Subject-3">Subject-1+Subject-2+Subject-3
<input type="checkbox" name="subject[]" value="Others">Others
<input type="submit" value="Submit" name="submit" class="wpcf7-submit"><br><br>
php part:
if(isset($_POST['submit']))
{
if(!empty($_POST['subject']) && $_POST['subject']=="Others") {
echo " <SCRIPT LANGUAGE='Javascript'>
window.alert('You wish to select other combo apart from given in above list. Kindly contact incharge of this institute.');
</SCRIPT>";
}
}
This code is not working. What changes should I make??
As subject[] is an array in
<input type="checkbox" name="subject[]" value="Others">
is an array type. So you should validate subject field as array as follows:
if (isset($_POST['submit'])) {
if (!empty($_POST['subject']) && in_array("Others", $_POST['subject'])) {
echo " <SCRIPT LANGUAGE='Javascript'>
window.alert('You wish to select other combo apart from given in above list. Kindly contact incharge of this institute.');
</SCRIPT>";
}
}
You can try something like given below. you are free to use in_array() but if you want to use the second condition with it use foreach like this.
if (in_array("Others", $_POST['subject'])) {
echo 'selected ';
}
else{
//do something
}
OR
if(isset($_POST['submit']))
{
$results=$_POST['subject'];
foreach ($results as $result) {
if ($result==='Others') {
echo 'selected ';
}
else{
//do something
}
}
}
<?php
if(isset($_POST['submit']) && $_POST['submit']=="Submit"){
if(isset($_POST['subject'])){
$subject = $_POST['subject'];
if(in_array('Others', $subject)){
echo "<script>alert('You wish to select other combo apart from given in above list. Kindly contact incharge of this institute.');</script>";
}
}else{
echo "<script>alert('You wish to select other combo apart from given in above list. Kindly contact incharge of this institute.');</script>";
}
}
?>
<form method="POST" action="">
<input type="checkbox" name="subject[]" value="Subject-1+Subject-2">Subject-1+Subject-2
<input type="checkbox" name="subject[]" value="Subject-1+Subject-2+Subject-3">Subject-1+Subject-2+Subject-3
<input type="checkbox" name="subject[]" value="Others">Others
<input type="submit" value="Submit" name="submit" class="wpcf7-submit"><br><br>
</form>

radio button cannot empty

This is my code for radio button. I want add condition, when one radio button not selected by user, then it will come out warning there is data empty. When complete, it will count sum.
<form action="" method="post">
Player 1:
male <input type="radio" name="gender[1]" value="1">
female <input type="radio" name="gender[1]" value="2">
<br>
Player 2:
male <input type="radio" name="gender[2]" value="1">
female <input type="radio" name="gender[2]" value="2">
<br>
Player 3:
male <input type="radio" name="gender[3]" value="1">
female <input type="radio" name="gender[3]" value="2">
<br>
<input type="submit" name="submit" value="submit">
</form>
<?php
if (isset($_POST['submit']))
{
$sum=0;
if (isset($_POST['gender']))
{
$gender=$_POST['gender'];
foreach ($gender as $value)
{
$sum=$sum+$value;
}
echo $sum;
}
else
{
echo 'you did not choose any genders';
}
}
?>
If all radio buttons are selected, count($_POST['gender']) would be 3. So just replace
else
{
echo 'you did not choose any genders';
}
with
if (count($_POST['gender']) < 3) {
echo 'you did not choose any genders';
}
It's simple, you can write
if(!$_POST['gender']) echo ....;
or, if you want to check single positions
if(!$_POST['gender'][0] && !$_POST['gender'][1] && !$_POST['gender'][2]) echo ...;

Save $_SESSION from a list of checkbox data?

Im trying to save $_SESSION data to keep checked values on page refresh, I have some code written up but I am missing a piece.
Session Code:
session_start();
if(isset($_POST['submit'])) {
//user posted variable
$checks = $_POST['personalization_result'];
if(isset($_POST['personalization_result'])) {
$_SESSION['value'] = $_POST['personalization_result']; }
else {
$_SESSION['value'] = '';
}
}
Form Code:
<form action="" method="post" id="question-form">
<input type="hidden" name="submit" value="1">
<li>
<input name="personalization_result[memory_0]" type="hidden" class="<?php echo $_SESSION['value']['memory_0'] ?>" value="0" />
<input name="personalization_result[memory_0]" type="checkbox" value="1" id="personalization_result_memory_0" />
</li>
<li>
<input name="personalization_result[memory_1]" type="hidden" class="<?php echo $_SESSION['value']['memory_1'] ?>" value="0" />
<input name="personalization_result[memory_1]" type="checkbox" value="1" id="personalization_result_memory_1" />
</li>
//There are many checkboxes, this is just two for demo purposes
<input class="submit" type="submit" value="Send" />
</form>
For testing I am echoing $_SESSION['value']['memory_0'] && $_SESSION['value']['memory_1']inside the hidden input, they return properly, if the input is checked the class is 1, if it's unchecked the class is 0.
I am not sure the best way to say if it = 1 then echo checked="checked"
My shotty attempt:
<input name="personalization_result[memory_0]" type="hidden" class="<?php echo $_SESSION['value']['memory_0'] ?>" value="0" />
<input name="personalization_result[memory_0]" type="checkbox" value="1" id="personalization_result_memory_0" <?php if($_SESSION['value']['memory_0'] = 1) {echo 'checked="checked"';} ?> />
This returns an error "Cannot use a scalar value as an array", that and I dont like how thats written up.
So with all the given information, whats the best way to write if my value = 1 return checked="checked"?
This will make your PHP much cleaner and scalable:
Use this function at the top:
session_start();
function checkbox($id)
{
$isChecked = '';
if (isset($_SESSION['value']['memory_'.$id]))
{
$isChecked = ($_SESSION['value']["memory_".$id] == 1) ? 'checked="checked"' : '';
}
echo '<input name="personalization_result[memory_'.$id.']" type="checkbox" ';
echo 'value="1" id="personalization_result_memory_'.$id.'" ';
echo $isChecked.' />';
}
and then instead of writing out <input ... <?php ... ?> ... /> for every single checkbox, just use
<?php checkbox(0); ?>
<?php checkbox(1); ?>
Your short attempt has an error:
instead of <?php if($_SESSION['value']['memory_0'] = 1) try <?php if($_SESSION['value']['memory_0'] == 1)

PHP Forms - Numeric Value of Checkbox

I am creating an online order form for multiple products. I need to calculate the total cost for the products selected via checkbox and send it as a confirmation e-mail. The value of the checkbox is the price in dollars.
<input type="checkbox" id="product1" name="product1" value="100" />
<input type="checkbox" id="product2" name="product2" value="250" />
In my 'process.php' file, I need to total the cost for all items if they are checked.
if(isset($_POST['product1']) && $_POST['product1'] == '100') {
   $product1 = 100;
}
if(isset($_POST['product2']) && $_POST['product2'] == '250') {
   $product2 = 250;
}
$dollars = $product1 + $product2;
When I try to do it this way, $dollars is an empty variable "". Can someone tell me how to fix this?
Thank you!
There is no syntactical error in your code. So the only explanation is that,
$_POST['product1'] does not have value 100 or they are not sent through post at all
$_POST['product2'] also does not have value 250 or they are not sent through post as well
In order to verify this, do a quick var_dump($_POST) at the top of your .php file
This works, so something does not work in your code but we cant see it right now
<?php
if(isset($_POST['submit']) ) {
if(isset($_POST['product1']) && $_POST['product1'] == '100') {
$product1 = 100;
}
if(isset($_POST['product2']) && $_POST['product2'] == '250') {
$product2 = 250;
}
echo $dollars = $product1 + $product2;
}
?>
<form method="post">
<input type="checkbox" id="product1" name="product1" value="100" />
<input type="checkbox" id="product2" name="product2" value="250" />
<input type="submit" name="submit" />
</form>
You can post an array of checkbox if you want. Try this :
<?php
$dollars = 0;
// If the user post the form
if(isset($_POST['value'])) {
foreach ($_POST['product'] as $product) {
if(is_numeric($product) && $product >= 0) {
$dollars += $product;
}
}
}
?>
<!DOCTYPE HTML>
<html>
<head></head>
<body>
<p>Product : <?php echo $dollars; ?></p>
<form method="post">
<label><input type="checkbox" name="product[]" value="100" /> 100</label><br/>
<label><input type="checkbox" name="product[]" value="250" /> 250</label><br/>
<label><input type="checkbox" name="product[]" value="350" /> 350</label><br/>
<label><input type="checkbox" name="product[]" value="20" /> 20</label><br/>
<label><input type="checkbox" name="product[]" value="25" /> 25</label><br/>
<input type="submit" name="value" />
</form>
</body>
</html>
This code gets an array of selected products and make the sum.
Checkbox values are not sent to the server with POST data if not checked. (edited post)
If you must POST this data, consider using a <input type="hidden" name="product1value" value="100"/> to send the value instead? Keep the check boxes just to see if they are ticked.

Categories