Small PHP Checkbox Dilemma

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

Show more of the code.

add this line to the top of you form handler, and you will understand the value being passed by the checkbox.


var_dump($_POST);

You test for that exact value (or the lack of it)

This 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\
]/", $name ) || preg_match( "/[\\r\
]/", $email ) ) {
   header( "Location: $errorurl" );
   exit ;
}

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


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

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




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


I would suggest that you start by gathering up your input then do your processing. Easier to read.



// Initialize
$want2book = false;
$date = null;
$time = null;

// Extract
if (isset($_POST['want2book'])) $want2book = true;  // Seldom need to check value for check boxes

if (isset($_POST['date'])) $date = trim($_POST['date'];

if (isset($_POST['time'])) $time = trim($_POST['time'];

// Now do some processing
if ($want2book)
{
    if (!$date || !$time)
    {
        die('Checked but no date or time');
    }
    else
    {
        // Do your stuff
    }
}

I went with this (which is working):


$want2book = isset($_POST['want2book']);
$date = isset($_POST['date']) ? trim($_POST['date']) : false;
$time = isset($_POST['time']) ? trim($_POST['time']) : false;

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

Any improvements that can be made on that I’d welcome to hear…

Thank you to all who have replied though, I appreciate it.