I am trying to figure out how to turn this nested if statement into a single if statement and having trouble getting it to run.
This is the original nested if statement:
if (isset($_REQUEST['gender']))
{
$gender = $_REQUEST['gender'];
if ($gender == 'M')
{
echo '<p><b>Good day, Sir!</b></p>';
}
elseif ($gender == 'F')
{
echo '<p><b>Good day, Madam!</b></p>';
}
else
{ // Unacceptable value.
$gender = NULL;
echo '<p class="error">Gender should be either "M" or "F"!</p>';
}
}
else
{ // $_REQUEST['gender'] is not set.
$gender = NULL;
echo '<p class="error">You forgot to select your gender!</p>';
}
This is my attempt:
if (isset($_REQUEST['gender']) && $gender == 'M')
{
$gender = $_REQUEST['gender'];
echo '<p><b>Good day, Sir!</b></p>';
}
elseif (isset($_REQUEST['gender']) && $gender == 'F')
{
$gender = $_REQUEST['gender'];
echo '<p><b>Good day, Madam!</b></p>';
}
elseif (isset($_REQUEST['gender']) && $gender != 'M' || (isset($_REQUEST['gender']) && $gender != 'F')
{ // Unacceptable value.
$gender = $_REQUEST['gender'];
$gender = NULL;
echo '<p class="error">Gender should be either "M" or "F"!</p>';
}
else
{ // $_REQUEST['gender'] is not set.
$gender = NULL;
echo '<p class="error">You forgot to select your gender!</p>';
}
I am not sure if it's because the $gender = $_REQUEST['gender']; is not executing, but any help would be appreciated.
You're checking variable $gender before you assign value to it. Following will help,
$gender = isset($_REQUEST['gender']) ? $_REQUEST['gender'] : "";
if ($gender == 'M')
{
echo '<p><b>Good day, Sir!</b></p>';
}
else if($gender == 'F')
{
echo '<p><b>Good day, Madam!</b></p>';
}
else if (!empty($gender) && ($gender != 'M' || $gender != 'F'))
{ // Unacceptable value.
echo '<p class="error">Gender should be either "M" or "F"!</p>';
}
else
{
echo '<p class="error">You forgot to select your gender!</p>';
}
A switch statement might be cleaner here.
switch ($_REQUEST['gender']) {
case null:
echo '<p class="error">You forgot to select your gender!</p>';
break;
case 'M':
echo '<p><b>Good day, Sir!</b></p>';
break;
case 'F':
echo '<p><b>Good day, Madam!</b></p>';
break;
default:
echo '<p class="error">Gender should be either "M" or "F"!</p>';
}
Here's the untested way I'd do this. I'd also do client side(javascript) checks first. I'm also presuming your gender field is text if is select you could take out the strtolower and trim.
<?php
if (empty($_REQUEST['gender']) || (strtolower(trim($_REQUEST['gender'])) != 'm' && strtolower(trim($_REQUEST['gender'])) != 'f')) {
//this should probably be checked client side for better usability, but better to check server side that we trust the data
echo '<p class="error">Please enter a valid Gender</p>';
} else {
$gender = strtolower(trim($_REQUEST['gender']));
echo '<p><b>Good day, ';
if ($gender == 'm') {
echo 'Sir!</b></p>';
} else {
echo 'Madam!</b></p>';
}
}
Related
$var1="exit";
$var2="run";
$var3="go";
if($var1 != '' && $var2 != '' && $var3 != '' ){
//first condtion
if($var1 == 'clear'){
echo 'clear';
}
else {
exit();
}
//2nd condtion
if($var2 == 'run'){
echo 'run';
}
//3rd condtion
if($var3 == 'go'){
echo 'go';
}
}
**I try to Exit() First If Condition, continue to next IF Condition, But Cannot Continue next IF condition totally Exit Overall Please Fix My Problem Thankyou **
No need to use else condition if you want to run second if statement
if($var1 != '' && $var2 != '' && $var3 != '' ){
//first condtion
if($var1 == 'clear'){
echo 'clear';
}
//2nd condtion
if($var2 == 'run'){
echo 'run';
}
//3rd condtion
if($var3 == 'go'){
echo 'go';
}
}
What About the idea of this one.
<?php
$var1="exit";
$var2="run";
$var3="go";
if($var1 != '' && $var2 != '' && $var3 != '' )
{
if($var2 == 'run'){
echo 'run';
}
elseif($var3 == 'go'){
echo 'go';
}
elseif($var1 == 'clear'){
echo 'clear';
}
else {
exit();
}
}
just use only if.. use return
$var1="exit";
$var2="run";
$var3="go";
if($var1 != '' && $var2 != '' && $var3 != '' ){
//first condtion
if($var1 == 'clear'){
echo 'clear';
}else {
return;
}
//2nd condtion
if($var2 == 'run'){
echo 'run';
}else {
return;
}
//3rd condtion
if($var3 == 'go'){
echo 'go';
}else {
return;
}
}
I trying to create a function that matches the first number in a string to a state. For example if the user inputs a number that starts with 3, and the state 'vic', then the form should not present any errors. However no matter what is entered the function always returns true. Any help would be appreciated.
$state = array('Please Select', 'VIC', 'NSW', 'QLD', 'NT', 'WA', 'SA', 'TAS', 'ACT'); //In the form this is a drop down menu
$selected_key = $_POST['state'];
$postcode = $_POST["postcode"]; //The full number entered by the user
$errMsg .= validatePS($postcode, $selected_key);
function validatePS($ps, $state) {
$errMsg ="";
$digit = $ps[0]; //Takes the first number from full postcode
$valid = false;
if (($digit == 3) or ($digit == 8) && ($state == 'vic'))
{
$post = true;
}
if (($digit == 1) or ($digit == 2) && ($state == 'nsw'))
{
$post = true;
}
if (($digit == 4) or ($digit == 9) && ($state == 'qld'))
{
$post = true;
}
if ($valid == false) {
$errMsg .= "<p>Match the correct postcode to state</p>";
}
return $errMsg;
}
if ($errMsg !=""){
echo "<p>Please correct the following errors...</p>"; //Prints out errors
echo "<p>$errMsg</p>";
}
You have two "flag" variables - $post which you are updating and $valid which you rely on. If you reduce them to one variable, you should get the behavior you want:
function validatePS($ps, $state) {
$errMsg ="";
$digit = $ps[0]; //Takes the first number from full postcode
$valid = false;
if (($digit == 3) or ($digit == 8) && ($state == 'vic'))
{
$valid = true;
}
if (($digit == 1) or ($digit == 2) && ($state == 'nsw'))
{
$valid = true;
}
if (($digit == 4) or ($digit == 9) && ($state == 'qld'))
{
$valid = true;
}
if ($valid == false) {
$errMsg .= "<p>Match the correct postcode to state</p>";
}
return $errMsg;
}
Note that you could clean up this code considerably by using logical operators instead of multiple if statements:
function validatePS($ps, $state) {
$errMsg ="";
$digit = $ps[0]; //Takes the first number from full postcode
$valid = false;
if ((($digit == 3) or ($digit == 8) && ($state == 'vic')) ||
(($digit == 1) or ($digit == 2) && ($state == 'nsw')) ||
(($digit == 4) or ($digit == 9) && ($state == 'qld'))) {
$errMsg .= "<p>Match the correct postcode to state</p>";
}
return $errMsg;
}
This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 8 years ago.
Header location not working on live server but works on localhost.
This code worked until last week, but it does not work anymore.
So, I have started testing on localhost. It is working as before.
I tried to add "ob_start()" on the top of the code; not working.
Please review this code and comment.
<?php session_start();
$fnameErr ="";
$lnameErr ="";
$emailErr ="";
$phoneErr = "";
$dateErr = "";
$timeErr = "";
$errMsg = "";
$area = "";
$local3 = "";
$local4 = "";
$cust_info = "";
$charOnly = "/^[a-z]+[a-z]$/i";
$reg_email = "/^[^0-9~!##$%^&*()_+=?.,][a-z0-9_]+([.][a-z0-9_]+)*[#][a-z0-9_]+([.][a-z0-9_]+)*[.][a-z]{2,3}$/i";
$reg_phone = "/^(\d{3}+\d{3}+\d{4}|\d{3}\d{3}+[\s]{1}+\d{4}|\d{3}+[\s]{1}+\d{3}+[\s]{1}+\d{4}||\d{3}+[-]{1}+\d{3}+[-]{1}+\d{4}|\d{3}+[\s]{1}+\d{7}|\(\d{3}\)\s{1}\d{3}[\s-]{1}\d{4})$/";/*"/^(\d{3}|[(]\d{3}[)]|\d{3}[)])[ -]*\d{3}[ -]*\d{4}$/";*/
$dataValid = true;
$phone = $area .''. $local3 .''. $local4;
$phoneValid = true;
// If submit with POST
if ($_POST) {
$errMsg = "Debugging";
$area = $_POST['c_area'];
$local3 = $_POST['c_local3'];
$local4 = $_POST['c_local4'];
$cust_info = array( "first" => $_POST['c_fname'],
"last" => $_POST['c_lname'],
"email" => $_POST['c_email'],
"phone" => array("area"=> $area,
"mid" => $local3,
"last" => $local4),
"date" => $_POST['c_date'],
"time" => $_POST['c_time']);
// Test for nothing entered in field
if ($_POST['c_fname'] == "") {
$fnameErr = "Please enter your first name.";
$dataValid = false;
}
else {
if ( preg_match($charOnly, $_POST['c_fname']) )
{
$fnameErr = "";
} else {
$fnameErr = "This is an invalid name.";
$dataValid = false;
}
}
if ($_POST['c_lname'] == "") {
$lnameErr = "Please enter your last name.";
$dataValid = false;
}
else {
if ( preg_match($charOnly, $_POST['c_lname']) )
{
$lnameErr = "";
} else {
$lnameErr = "This is an invalid name.";
$dataValid = false;
}
}
if ($_POST['c_email'] == "") {
$emailErr = "Please enter E-mail address.";
$dataValid = false;
}
else {
if ( preg_match($reg_email, $_POST['c_email']) )
{
$emailMsg = "";
} else {
$emailMsg = "E-mail is not Valid.";
$dataValid = false;
}
}
if ($_POST['c_area'] == "") {
$phoneErr = "Please enter phone number.";
$dataValid = false;
$phoneValid = false;
}
if ($_POST['c_local3'] == "") {
$phoneErr = "Please enter phone number.";
$dataValid = false;
$phoneValid = false;
}
if ($_POST['c_local4'] == "") {
$phoneErr = "Please enter phone number.";
$dataValid = false;
$phoneValid = false;
}
if( $phoneValid ) {
$phone = $area . "" . $local3 . "" .$local4;
if ( preg_match($reg_phone, $phone) ) {
$phoneErr = "";
} else {
$phoneErr = "Phone number is not Valid.";
$dataValid = false;
}
} else {
$area = "";
$local3 = "";
$local4 = "";
$phone = "";
}
if ($_POST['c_date'] == "") {
$dateErr = "Please choose a date.";
$dataValid = false;
}
if ($_POST['c_time'] == "" || $_POST['c_time'] == "Morning" || $_POST['c_time'] == "Afternoon") {
$timeErr = "Please choose a time.";
$dataValid = false;
} else {
if ("07:00" == $_POST['c_time']){
$Checked0700 = 'selected';
}
else if ("07:30" == $_POST['c_time']){
$Checked0730 = 'selected';
}
else if ("08:00" == $_POST['c_time']){
$Checked0800 = 'selected';
}
else if ("08:30" == $_POST['c_time']){
$Checked0830 = 'selected';
}
else if ("09:00" == $_POST['c_time']){
$Checked0900 = 'selected';
}
else if ("09:30" == $_POST['c_time']){
$Checked0930 = 'selected';
}
else if ("10:00" == $_POST['c_time']){
$Checked1000 = 'selected';
}
else if ("10:30" == $_POST['c_time']){
$Checked1030 = 'selected';
}
else if ("11:00" == $_POST['c_time']){
$Checked1100 = 'selected';
}
else if ("11:30" == $_POST['c_time']){
$Checked1130 = 'selected';
}
else if ("12:00" == $_POST['c_time']){
$Checked1200 = 'selected';
}
else if ("12:30" == $_POST['c_time']){
$Checked1230 = 'selected';
}
else if ("13:00" == $_POST['c_time']){
$Checked1300 = 'selected';
}
else if ("13:30" == $_POST['c_time']){
$Checked1330 = 'selected';
}
else if ("14:00" == $_POST['c_time']){
$Checked1400 = 'selected';
}
else if ("14:30" == $_POST['c_time']){
$Checked1430 = 'selected';
}
else if ("15:00" == $_POST['c_time']){
$Checked1530 = 'selected';
}
else if ("15:30" == $_POST['c_time']){
$Checked1530 = 'selected';
}
else if ("16:00" == $_POST['c_time']){
$Checked1600 = 'selected';
}
else if ("16:30" == $_POST['c_time']){
$Checked1630 = 'selected';
}
else if ("17:00" == $_POST['c_time']){
$Checked1700 = 'selected';
}
else if ("after" == $_POST['c_time']){
$Checkedafter = 'selected';
}
}
}
if ($_POST && $dataValid) {
$_SESSION['token1'] = "ok";
$_SESSION['cust'] = $cust_info;
header('Location:innout-booking-step2.php');
exit();
?>
I also faced such problem so I tried following steps to resolve it.
1. Remove or comment spaces, echos, print_r, error reporting before calling header location.
2. Remove spaces after php end tag (after ?> )
3. Modify header location syntax for this what I generally do is open w3schools copy header location syntax and paste it. In your case you should try to change your header code as
header('Location: innout-booking-step2.php'); (space after : )
When I click y link,it is going to x.Why is that ?
x
y
<?php
if(isset($_REQUEST['hello']) == 'x')
{
echo 'x';
}
else if(isset($_REQUEST['hello']) == 'y'){
echo 'y';
}
else
{
echo "else";
}
try
if(isset($_REQUEST['hello']) && ($_REQUEST['hello']) == 'x') )
The isset function returns either true or false and you are comparing that return value with strings 'x' and 'y'.
Since you are using == and not ===, true == 'x' will return ture.
To fix this first you need to check if the variable is set and only then compare it.
if(isset($_REQUEST['hello']) && ($_REQUEST['hello']) === 'x'))
isset returns true or false, in both of these examples hello is set to something, so isset would return true (which does not equate to either x or y)
Hopefully this helps.
<?php
if(isset($_REQUEST['hello']) && $_REQUEST['hello']== 'x')
{
echo 'x';
}
else if(isset($_REQUEST['hello']) && $_REQUEST['hello'] == 'y'){
echo 'y';
}
else
{
echo "else";
}
?>
isset will check whether the request is set or not returns 0 or 1
x
y
<?php
if($_REQUEST['hello'] == 'x')
{
echo 'x';
}
else if($_REQUEST['hello'] == 'y'){
echo 'y';
}
else
{
echo "else";
}
<?php $myvar = $_REQUEST['hello'];
if($myvar == 'x')
{
echo 'x';
}
else if($myvar == 'y')
{
echo 'y';
}
else
{
echo 'else';
}
?>
Try this one
I have a PHP script and MSSQL tables, I got the answer key in a variable stored in $right_answer and user selected answers in $user_answer_select THe format is something like this
5+10?
A) 10
B) 15
C) 20
D) 25
E) 50
Answer key: B
what I want to do is put a check mark next to B if its correct and a X if it is wrong, how would I make the if else statements here?
This is the code I currently have
if(($user_answer_select == $right_answer) && $user_answer_select == 'a') $a_sel = "<img src=\"tick_icon.gif\">";
else if(($user_answer_select == $right_answer) && $user_answer_select == 'b') $b_sel = "<img src=\"tick_icon.gif\">";
else if(($user_answer_select == $right_answer) && $user_answer_select == 'c') $c_sel = "<img src=\"tick_icon.gif\">";
else if(($user_answer_select == $right_answer) && $user_answer_select == 'd') $d_sel = "<img src=\"tick_icon.gif\">";
else if(($user_answer_select == $right_answer) && $user_answer_select == 'e') $e_sel = "<img src=\"tick_icon.gif\">";
This is wrong because some of the questions that don't have answers for are highlighted as true. What's the way to do this?
$answers = array ( "A"=>10, "B"=>15, "C"=>20, "D"=>25, "E"=>50 );
$right_answer = "B";
$user_selected_answer = "A";
echo "5+10?<br/>";
foreach ($answers as $key => $value) {
echo $key.") ".$value;
if ($value === $user_selected_answer) {
if ($value === $right_answer){ echo "check!"; }
else { echo "X"; }
}
echo "<br/>";
}
echo "Answer key: $right_answer";
if ( $user_answer_select == $right_answer ) {
$correct = true;
} else {
$correct = false;
}
Then in the correct answer on the form:
<?php echo $correct == true ? 'x' : ''; ?>