Jump to content

Checking if a checkbox is checked? help!


Deanznet

Recommended Posts

 

I have a form that post to register.php

 

 

 

Register.php

 

 


if ($_POST['fname']=='' || strlen($_POST['fname'])<3)
{
	$errors[] = 'First name is required and must contain 3 characters or more';
}



if ($_POST['lname']=='' || strlen($_POST['lname'])<3)
{
	$errors[] = 'Last name is required and must contain 3 characters or more';
}


if ($_POST['spassword']=='' || alpha_numeric($_POST['spassword'])==FALSE)
{
	$errors[] = 'A password is required and must be alpha-numeric';
}

if ($_POST['spassword']!=$_POST['passwordagain'])
{
	$errors[] = 'The two passwords must match';
}

if (valid_email($_POST['semail'])==FALSE)
{
	$errors[] = 'Please supply a valid email address';
}

if(is_array($errors))
{
	echo '<p class="error"><b>The following errors occured</b></p>';
	while (list($key,$value) = each($errors))
	{

		echo '<span class="error"><ul><li>'.$value.'</span><br /></ul>';
	}
}
else {

	echo '<p><b>Success!</b></p>';
	echo '<span>Your registration was successfully processed. You may login and start using your account. Thank you for registering !</span>';
}

 

   <input id="terms-chk" name="terms" value="checkbox" type="checkbox" />

 

 

Im trying to make it if its not check to do an error.. It tired

if($_POST['terms-chk'] == "off")

{

$errors[] = 'You must agree to the Terms of Use.';

}

 

But that did not work.

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

Forgive me if I am incorrect, but I don't think that would work, premiso.

 

The terms-chk will be included in the post array whether it's checked or not. You'd only be able to check if it existed with your code. If I am correct, the only way to check if a checkbox is actually checked is to compare it with its value.

<?php
if (isset($_POST['check1'])) {
echo $_POST['check1'] . " check1 was checked <br />";
}
if (isset($_POST['check2'])) {
echo $_POST['check2'] . " check2 was checked <br />";
}

echo <<<OUTPUT
<form action="test.php" method="POST">
<input type="checkbox" name="check1" value="check1" /><br />
<input type="checkbox" name="check2" value="check2" />
<input type="submit" value="Submit" />
</form>
OUTPUT;

?>

 

Nope, isset works just fine as I suspected. :) Give it a shot.

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.