i am having trouble in checking if a form field is empty and redirecting it to a particular location depending on whether the if condition is true or false. The problem is even after filling all the fields the 2nd if condition always becomes false and popup.php page gets opened. This is my php code :
<?php
if(isset($_POST['YourName']) && isset($_POST['EmailID']) && isset($_POST['ContactNumber']) && isset($_POST['Destination']) && isset($_POST['ModeTransport']) && isset($_POST['DateTravel']) && isset($_POST['DateReturn']) && isset($_POST['Accommodation']) && isset($_POST['Choice1']) && isset($_POST['Choice2']) && isset($_POST['Adults']) && isset($_POST['Child']) && isset($_POST["how_to_reach_you"]))
$YourName=$_POST['YourName'];
$EmailID=$_POST['EmailID'];
$ContactNumber=$_POST['ContactNumber'];
$Destination=$_POST['Destination'];
$ModeTransport=$_POST['ModeTransport'];
$DateTravel=$_POST['DateTravel'];
$DateReturn=$_POST['DateReturn'];
$Accommodation=$_POST['Accommodation'];
$Choice1=$_POST['Choice1'];
$Choice2=$_POST['Choice2'];
$Adults=$_POST['Adults'];
$Child=$_POST['Child'];
$via=$_POST["how_to_reach_you"];
if(!empty($YourName) && !empty($EmailID) && !empty($ContactNumber) && !empty($Destination) && !empty($ModeTransport) && !empty($DateTravel) && !empty($DateReturn) && !empty($Accommodation) && !empty($Choice1) && !empty($Choice2) && !empty($Adults) && !empty($Child))
if($via=="Email me")
header("Location:email.php");
else
header("Location:call.php");
else
header("Location:popup.php");
?>
well as I said isset is irrelevant, and to handle the empty I would do this
$via = (isset($_POST["how_to_reach_you"]) ? $_POST["how_to_reach_you"] : false;
unset($_POST["how_to_reach_you"]);
$post = array_filter($_POST);
if( count($post) == count($_POST)){
if($via=="Email me"){
header("Location:email.php");
}else{
header("Location:call.php");
}
}else{
header("Location:popup.php");
}
None of those values are used in this code, and with a redirect they are pointless. Array filter removes empty elements from the array, so by using it you can check the count on the original and the filtered version to tell if any elements are empty. If there are empty values the $post
will be less the the $_POST
. Your not checking the how_to_reach_you
value fo
if you still need to declare all those variables you can use
extract($post);
Which will create variables with the same name as the array keys.