detect unchecked checkbox php - php

Is there a way to check of a checkbox is unchecked with php? I know you can do a hidden field type in html but what about with just php when the form is submitted? I tried below no luck.
if(!isset($_POST['server'])||$_POST['server']!="yes"){
$_POST['server'] == "No";
}

If a checkbox is not checked it will not be posted. if(!isset($_POST['checkboxname'])) will do the trick.
Be aware, though, you should at least submit something so that you know the form was submitted in the first place.
if (isset($_POST['formWasSubmitted'])) {
//form was submitted...let's DO this.
if (!isset($_POST['checkboxname'])) {
// checkbox was not checked...do something
} else {
// checkbox was checked. Rock on!
}
}

This is an old question, but for people looking for this....
Better approach to Matt's answer is to use $_SERVER['REQUEST_METHOD'] to check if form was submitted:
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
//form was submitted...let's DO this.
if (!isset($_POST['checkboxname'])) {
// checkbox was not checked...do something
} else {
// checkbox was checked. Rock on!
}
}

$checkedfeild = #$_POST["yourfeildname"];
if(isset($checkedfeild))
{
//Code here
}
else
{
echo"Not checked";
}

Try this:
$checked = $_POST['notif'];
foreach($checked as $ch){
if($ch == $i){
/add element to checked set
$array_checked[]=$ch;
}
}
for($i=1;$i<6;$i++){
if(in_array($i,$array_checked)){
//do for checked
}else{
//do for unchecked
}
}

Related

simple yes or no checkbox for forms

I have inserted a checkbox in my form.
My code:
<input type="checkbox" id="checkbox" name="checkbox" value="1"/>
if($checkbox = ($_POST['checkbox']) == '1')
{
$checkbox = "si";
}
else
{
$checkbox = "no";
}
I would like that if the checkbox is checked i receive "yes" otherwise "no".
Thanks.
You've written wrong if condition here, You cannot use assignment in conditions.
Also there is no need to assign value to any variable in checking condition, You can directly use $_POST['checkbox']. Like this,
if($_POST['checkbox'] == '1') {
$checkbox = "si";
} else {
$checkbox = "no";
}
Update:
A better option is to use isset() which determine if a variable is set and is not NULL. Like this,
if(isset($_POST['checkbox'])) {
$checkbox = "si";
} else {
$checkbox = "no";
}
Program will go in if condition only when user has checked the checkbox. In above case value attribute for <input> is not required. So your HTML will look something like this,
<input type="checkbox" id="checkbox" name="checkbox"/>
$request['checkbox_name'] = $request['checkbox_name'] == null ? 'N' : 'Y'; //for default value

PHP If / Else statement going directly to the Else without waiting for form input

Ok so I have a form with 1 input and a submit button. Now I am using an if/else statement to make three acceptable answers for that input. Yes, No, or anything else. This if/else is working the thing is the code is kicking out the else function as soon as the page is loaded. I would like there to be nothing there until the user inputs then it would show one of three answers.
Welcome to your Adventure! You awake to the sound of rats scurrying around your dank, dark cell. It takes a minute for your eyes to adjust to your surroundings. In the corner of the room you see what looks like a rusty key.
<br/>
Do you want to pick up the key?<br/>
<?php
//These are the project's variables.
$text2 = 'You take the key and the crumby loaf of bread.<br/>';
$text3 = 'You decide to waste away in misery!<br/>';
$text4 = 'I didnt understand your answer. Please try again.<br/>';
$a = 'yes';
$b = 'no';
// If / Else operators.
if(isset($_POST['senddata'])) {
$usertypes = $_POST['name'];
}
if ($usertypes == $a){
echo ($text2);
}
elseif ($usertypes == $b){
echo ($text3);
}
else {
echo ($text4);
}
?>
<form action="phpgametest.php" method="post">
<input type="text" name="name" /><br>
<input type="submit" name="senddata" /><br>
</form>
You just need to call the code only when the POST value is set. This way it will only execute the code when the form was submitted (aka $_POST['senddata'] is set):
if(isset($_POST['senddata'])) {
$usertypes = $_POST['name'];
if ($usertypes == $a){
echo ($text2);
}
elseif ($usertypes == $b){
echo ($text3);
}
else {
echo ($text4);
}
}
Just put the validation in the first if statement like this:
if(isset($_POST['senddata'])) {
$usertypes = $_POST['name'];
if ($usertypes == $a) {
echo ($text2);
} elseif ($usertypes == $b) {
echo ($text3);
} else {
echo ($text4);
}
}
When you load your page the browser is making a GET request, when you submit your form the browser is making a POST request. You can check what request is made using:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Your form was submitted
}
Put this around your form processing code in order to keep it from being executed on GET request.

Issue w/PHP syntax

I need a simple php function to mimic an Ajax form submission - basically in the form there is a radio button "ajax" and it is either set to yes or no. I just need to mimic successful/failing ajax calls...
HTML
<label for="">Ajax Success*<input type="radio" name="ajax" id="yes" value="yes" checked>Yes<input type="radio" name="ajax" id="no" value="no">No</label>
PHP
<?php
$ajax = $_POST["ajax"];
if(isset($_POST['ajax'] == "yes")) {
echo "success";
} else {
echo "failure";
}
?>
if I remove the isset, I get an 'undefined index' error, if I put it in I get a syntax error but it looks correct to me...
I just need to send back an echo depending on what option is selected for the input 'ajax'
thx
isset($_POST['ajax'] == "yes") doesn't make sense. You want to check if it's set, and then check if its value is equal to "yes":
if(isset($_POST['ajax']) && $_POST['ajax'] == "yes") {
echo "success";
} else {
echo "failure";
}
As your code does, I'll say you use your defined variable like in this example:
<?php
$ajax = $_POST["ajax"];
if($ajax == "yes") {
echo "success";
} else {
echo "failure";
}
?>
Because if the variable has the value "yes" it'll be ok and undefined or other value will end in "failure".

Passing selected checkbox value array?

I have a checkbox down below...
It is in the loop :
<script>
function checkCheckBoxes_abel() { //check if the checkbox is checked before submitting.
if (document.payform.pay_checkbox.checked == false)
{
alert ('You didn\'t choose any of the checkboxes for payment !');
return false;
}
else
{
alert ('One or more checkboxes from payment form are checked!');
document.forms["payform"].submit();
return true;
}
}
</script>
<form name="payform" onsubmit="return checkCheckBoxes_abel();" method="POST" action="payment.php">
for($record_count=0;$record_count<$record;$record_count++)
{
<td><input type="checkbox" name="pay[]" id="pay_checkbox" value="<?php echo $amount_dueArr[$record_count];?>" onClick="checkTotal()"/></td>
}
</form>
How can I pass the value of the checkbox that is being selected ?
Thanks
can I do :
if (isset($_POST['pay']))
{
foreach($_POST["eg_payamt_"] as $key => $payamt){
echo "eg_payamt_$key => $payamt\n <br>";
}
}
on payment.php ?
Thanks
An illustration :
I have three checkboxes...
If I check one of the checkbox,
Checkbox ticked on : Array
and if I'm not checking any of them
Checkbox ticked on :
Which is correct, but the content of the Array is not only one but three of them,
How can I make it only one ? or only two ? depends on how many checkboxes are being checked.
can I do it on another field ?
it seems that it works only for one field
if (isset($_POST['pay']))
{
if(is_array($_POST['pay']))
{
//foreach($_POST["pay"] as $key => $desc)
foreach($_POST["eg_description_"] as $key => $desc)
{
echo "eg_description_$key => $desc\n <br>";
}
}
else
{
//echo 'description :'.$_POST['pay'];
echo 'description :'.$_POST["eg_description_"];
}
}
there are 2 types of values will receive in POST, if someone selects only one checkbox that will throw warning in foreach loop so you can try this way
if (isset($_POST['pay']))
{
if(is_array($_POST['pay'])) {
//foreach($_POST["eg_payamt_"] as $key => $payamt){
foreach($_POST["pay"] as $key => $payamt){
echo "eg_payamt_$key => $payamt\n <br>";
}
}
else {
echo 'pay : '. $_POST['pay'];
}
}
Check these -
http://www.kavoir.com/2009/01/php-checkbox-array-in-form-handling-multiple-checkbox-values-in-an-array.html
http://www.html-form-guide.com/php-form/php-form-checkbox.html

PHP actions if checkboxes are selected!

I have a form with 3 checkboxes, and my idea is run some commands if they are selected and do something else if they are not selected.
for($i=1; $i<=3; $i++)
{
if ($_POST['option'.$i])
{
echo "123";
}
if (!$_POST['option'.$i])
{
echo "456";
}
}
But if they are not selected the commands are not executed.. The if statement is correct?
No, what you should be doing is checking them like this:
if (isset($_POST['option'.$i]))
Otherwise, you are just trying to evaluate the boolean form of whatever is in that $_POST element. Why is this bad? Suppose the value of that field were 0. Even though the checkbox was checked, your code wouldn't run.
Documentation for isset()
Sure, that will work just fine.
If you want to slightly de-uglify your code, you can do it this way:
<input type="checkbox" name="options[option_name]" value="1" />
<input type="checkbox" name="options[second_option_name]" value="1" />
if(isset($_POST['options']) && is_array($_POST['options']) {
foreach($_POST['options'] as $option_name => $ignore_this) {
switch($option_name) {
case 'option_name':
// do something
break;
case 'second_option_name':
// do something else
break;
}
}
}
You can use an if ... else:
if ($_POST['option'.$i])
{
echo "123";
}
else
{
echo "456";
}
to avoid checking the same condition twice.

Categories