Hi all,

I have this PHP script (for a HTML form) which I would like to a) check if a checkbox is checked, and then b) if it is, to check some text fields for input. If the checkbox is not checked, these fields do not need to be checked.

This is my checkbox HTML:

<input id="want2book" type="checkbox" name="want2book" value="Yes" tabindex="11" />

..and this is the section of my PHP script which I can not get working (notice the commented out line above - this was an alternative attempt to get this to work):

//if($want2book == 'Yes'){ 

if($_POST['want2book'] == 'Yes')
{
	if (empty($date) || empty($time)) {
	   header( "Location: $errorurl" );
	   exit ;
	} 
}

To summarise: If the checkbox 'want2book' is checked, I would like the script to make sure there is content in the two text fields 'date' and 'time' - if there is, great, submit and send out the email. If not, redirect the user to the $errorurl.

At present, the code above sends the email regardless of the check state and any input or not into the date and time fields. This is so simple but I just cant seem to grasp how to make it work...

Any help would be greatly appreciated

I'm not sure whether you've already set the values of $date and $time...

if(isset($_POST['want2book']) && $_POST['want2book'] == 'Yes') {
    if(! strlen(trim($_POST['date'])) || ! strlen(trim($_POST['time']))) {
        header("Location: {$errorurl}");
        exit(0);
    }
}

Thanks blogblue but that hasn't work either :(

Here is my full script:

<?php

$subject = "New Website Enquiry" ;
$formurl = "contact.php" ;
$errorurl = "error.php" ;
$thankyouurl = "thanks.php" ;


$name = $_POST['name'] ;
$tel = $_POST['tel'] ;
$email = $_POST['email'] ;

$want2book = $_POST['want2book'] ;
	$date = $_POST['date'] ;
	$time = $_POST['time'] ;


$genenq = $_POST['genenq'] ;
$http_referrer = getenv( "HTTP_REFERER" );


if (!isset($_POST['email'])) {
	header( "Location: $formurl" );
	exit ;
}
if (empty($name) || empty($tel) || empty($email)) {
   header( "Location: $errorurl" );
   exit ;
}



// ----------
// This is the section not working - check if checkbox is checked, if so, are date/time empty?

if(isset($_POST['want2book']) && $_POST['want2book'] == 'Yes') {
    if(! strlen(trim($_POST['date'])) || ! strlen(trim($_POST['time']))) {
        header("Location: {$errorurl}");
        exit(0);
    }
} 

// ----------


if ( preg_match( "/[\r\n]/", $name ) || preg_match( "/[\r\n]/", $email ) ) {
   header( "Location: $errorurl" );
   exit ;
}

$message =
	"Website Enquiry \n\n" .
	
	"Name:		$name \n\n" .
	"Tel: 		        $tel \n" .
	"E-mail:		$email \n\n\n" .


        "-- Booking info (if applicable): -- \n\n".

	"Preferred date: 	$date \n" .
	"Preferred time: 	$time \n" ;




mail(	//	"webmaster@want2party.com",
		"$subject", "$message" \n.
		"From: \"Website\" ;$email&gt;\nReply-To: \"$name\" <$email>\nX-Mailer: chfeedback.php 2.01", 
		"-f".$_REQUEST[email]);
header( "Location: $thankyouurl" );
?>

Let me get this right...

If the want2book checkbox is checked, you want to make sure there are values in the date and time fields.

If it isn't checked, you don't care if there are values in the date and time fields?

Because the code I posted should only work for this situation. When a checkbox isn't checked, it doesn't post a value at all, hence $_POST will not be set in the post array.

To force it to always post a value, I usually do:

<input type="hidden" name="want2book" value="No" />
<input id="want2book" type="checkbox" name="want2book" value="Yes" />

If the want2book checkbox is checked, you want to make sure there are values in the date and time fields.

If it isn't checked, you don't care if there are values in the date and time fields?

Yes - this correct

See the additional time/date fields aren't revealed to the user unless this checkbox is checked, so there's no need to check for a value on it, only to validate against empty time and date fields (which Id like to make required) - on checking of the checkbox.

Anything im trying at the moment though submits and sends regardless of empty fields and a checked checkbox.

What about:

$error_url = 'error.php';
$book = isset($_POST['want2book']);
$date = isset($_POST['date']) ? trim($_POST['date']) : false;
$time = isset($_POST['time']) ? trim($_POST['time']) : false;

if($book && (! $date || ! $time)) {
    header("Location: {$error_url}");
    exit(0);
}

This should work on the basis that a checkbox field, when not checked will not post a value. Thus giving you a true / false value on checking isset. Removing white space from the date and time fields if set.

I can't see any reason why this shouldn't work...

@BlocBlue - Thank you soo much pal, that works a treat!!

I'm not sure if the Trim function is working correctly though; "24th Jan 2013" still submits as 24th Jan 2013 (not "24thJan2013" as expected, is that correct? Luckily it doesnt matter to me as jQuery date/timepicker will see that the input is formatted correctly, I just wondered whether it was supposed to strip the input of all white space/space bar presses?

Many many thanks once again!

Glad it worked. Please mark the thread as solved :)

And FYI - trim only removes white space from the end of strings. Not from the middle.

Aah cool, thanks for that tidbit.

..and will do! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.