Jump to content

Checking if checkbox is not checked


jaArch

Recommended Posts

Hi:

 

How do I check to see if a checkbox has not been checked? Would the same rules apply to a radiobutton?

 

This is what I have:

 

<input name="status[1]" id="approved" value="<?php echo $_POST['approved'] ?>" type="checkbox"  /> <label for="approved">Approved</label>

 

if (isset($_POST['status[1]'])):
$errMsg['status[1]'] = "Must select a status.";
endif;

 

I tested it out, but it just prints the error message even if I have checked it

 

Link to comment
https://forums.phpfreaks.com/topic/250597-checking-if-checkbox-is-not-checked/
Share on other sites

That one didn't really work. So I tried this:

 

foreach($_POST['location'] as $value){
if (isset($_POST['value'])):
$succMsg['location'] = "LOCATION = {$_POST['location']}";
else:
$errMsg['location'] = "Must select a location.";
endif;
}

 

<input name="location[]" id="radio1" value="<?=$_POST['garage'] ?>" type="radio" /> <label for="radio1">Garage</label>
		<input name="location[]" id="radio2" value="<?=$_POST['home'] ?>" type="radio" /> <label for="radio1">Home</label>

 

But when I test it out it only prints "Array" instead of what I originally selected. Could you help me  with this part as well?

 

Thanks

foreach($_POST['location'] as $value){

That implies that your $_POST['location'] is an array, but later you try to use it as a string.  Perhaps you meant to use $value directly?

 

Yeah, I've tried that:

 

foreach($_POST['location'] as $value){
if (isset($_POST['value'])):
$succMsg['location'] = "LOCATION = $value";
else:
$errMsg['location'] = "Must select a location.";
endif;
}

 

It just says LOCATION = when I select something

 

do this:

 

echo "<pre>" . print_r($_POST,1) . "</pre>";

 

That will show you what the actual contents of $_POST are, including any nested arrays.  Work from there.  We can't keep guessing at what the proper structure of your code is without knowing what's coming into post.

 

-Dan

Well I tried to switch it up a bit and am officially stumped...here's my entire code:

 

 

<?

$PROVINCES = array(	"--" => "--- Please Select Provinces ---",
"nf"=>"Newfoundland",
"pe"=>"Prince Edward Island",
"nb"=>"New Brunswick",
"ns"=>"Nova Scotia",
"qc"=>"Quebec",
"on"=>"Ontario",
"mb"=>"Manitoba",
"sk"=>"Saskatchewan",
"ab"=>"Alberta",
"bc"=>"British Columbia",
"nt"=>"Northwest Territories");

if ($_SERVER['REQUEST_METHOD'] == 'POST'):


if (empty($_POST['name'])):
$errMsg['name'] = "Name cannot be empty.";
else: 
$succMsg['name'] = "NAME = {$_POST['name']}";
endif;

if (empty($_POST['city'])):
$errMsg['city'] = "City must not be empty.";
else:
$succMsg['city'] = "CITY = {$_POST['city']}";
endif;

if (isset($_POST['status1'])):
$succMsg['status1'] = "STATUS = {$_POST['status1']}";
else:
$errMsg['status1'] = 'Must select a status.';
endif;

foreach($_POST['location'] as $value){
if (isset($_POST['value'])):
$succMsg['location'] = "LOCATION = ".$_POST['value'];
else:
$errMsg['location'] = "Must select a location.";
endif;
}

if (isset($_POST['province'])):
$succMsg['province'] = "PROVINCE = {$_POST['province']}";
else:
$errMsg['province'] = "Must select a province.";
endif;


if(ereg('(Mr\.|Mrs\.)',$_POST['name']) == false):
$errMsg['fullname'] = "Fullname not entered correctly.";
endif;

if (count($errMsg) > 0):
$dispErrMsgs = true;
else:
$dispSuccessMsg = true;
endif;
endif;
?>

<html>
<head>
</head>
<body>
<p><a href="<?= $_SERVER['PHP_SELF'] ?> ">Refresh This Page</a></p> 
<? if ($dispErrMsgs): ?>
            <p>There are errors in the code: </p>
            <ol>
                <? foreach($errMsg as $errMsgs ): ?>
                <li><?= $errMsgs ?></li>
                <? endforeach; ?>
            </ol>
		<? endif; ?>
		 <? if ($dispSuccessMsg): ?>
            <p>Thank you for your submission. </p>
		<ol>
		<? foreach($succMsg as $succMsgs ): ?>
		<li><?= $succMsgs ?></li>
		<? endforeach; ?>
        <? endif; ?>


<form action="<?= $_SERVER['PHP_SELF'] ?>"  name="getstuff" method="post" >
            <input type="hidden" name="_act" value="post">

		<label>Name</label>
		<p>
		<input name="name" type="text" class="textbox"
		value="<? $_POST['name']; ?>" size="20">
		</p>

		<label>City</label>
		<p>
		<input name="city" type="text" class="textbox"
		value="<? $_POST['city']; ?>" size="20">
		</p>

		<label>Location</label>	
		<p>
		<input name="location1" id="radio1" value="<?=$_POST['garage'] ?>" type="radio" /> <label for="radio1">Garage</label>
		<input name="location2" id="radio2" value="<?=$_POST['home'] ?>" type="radio" /> <label for="radio1">Home</label>
		</p>

		<label>Provinces</label>
		<p>
		<select name = "province[]" multiple size = "12" id="type" >
                                <? foreach ($PROVINCES as $abbreviation => $longname){ ?>
                                <option value = "<?= $abbreviation ?>"> <?= $longname ?></option>
                                <? } ?>
                            </select>
						</p>

		<label>Select status</label>
		<p>
		<input name="status1" id="approved" value="<?= $_POST['approved'] ?>" type="checkbox"  /> <label for="approved">Approved</label>
            <input name="status2" id="pending" value="<?=$_POST['pending'] ?>" type="checkbox"  /> <label for="pending">Pending Application</label>				<input name="status[]" id="actives" value="actives" type="checkbox"  /> <label for="actives">Active Service</label>
		</p>

		<p>
		<input name="_submit" type="submit" class="button" value="Submit"> 
		</p>

</form>

<?
$a = ('name');
$b = ('city');

$trim = ($a);
$trim = ($b);
echo "<pre>" . print_r($_POST,1) . "</pre>";
?>
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.