PHP validating query string if statement - php

This seems like it should be easy but I can't for the life of me get this to work, I'm checking a variable in a query string, if it returns a Yes it redirects to a new page. If it returns a no I want it to display an error message above the form.
My issue is with displaying the error message, it redirects when var=Yes but when it gives me var=no I can't get it to display the error message. If I replace $errormsg with header(..); it works so I know it's not my if statement.
if (isset($_GET['var'])) {
$var = $_GET['var'];
$errormsg = '';
if ($var == 'Yes') {
header( 'Location: http://www.google.com' ); exit;
}
else if ($var == 'no') {
$errormsg = 'This is an error message.';
}
};
And this is the error message above the form:
<div class="errormsg"><?php echo $errormsg; ?></div>
My PHP ability is limited and I can't figure out what I'm doing wrong, any help will be appreciated.
Thanks!

My guess is the $var is not returning what you are expecting (possibly a "No" instead of a "no" as mjayt suggested in the comments.
First step would be to debug, and determine what $var is actually returning. Try simple print statements and go from there.

Try to echo $errormsg directly under the if block without any HTML. See if your text is returned.
Think you might remove that semi colon from that last curly brace also

Related

checking value of if conditino isn't printing correct output

I am working on a chat room and the condition i want to implement is:
when user send both file and text message it will return an error.
but this isn't working correct.
this is my code
if($_FILES['chat_upload_file']){
echo "You are in file checking<br/>";
echo $text_message;
if($text_messege){
echo "It's in IF<br/>";
$error = 1;
$error_msg = "Either Sender can send file or text";
exit(0);
}
else{
echo "it's in else<br/>";
exit(0);
}
here $text_messege = "hello user";
the output that i want is from internal IF : It's in IF
but the output is : It's in else
try
if($text_messege == "hello user"){
this will evaluate that $text_message is set to "hello user"
if($text_messege){
merely evaluates that $text_message is set and is set to something other than null or false.
Try this:
if(!empty($_FILES['chat_upload_file']) && !empty($text_messege)){
//both
}else if(!empty($_FILES['chat_upload_file'])){
//file only
}else if(!empty($text_messege)){
//message only
}else{
//nither
}
I would probably use empty instead of just testing if it's true or false. Empty will cover undefined index issues you may have. Without more to go on, I have no way to know if that is a possibility. For example I have no Idea where $message comes from and if it's always considered "set" (or not undefined).
You can optimize this by doing something like this.
$file = !empty($_FILES['chat_upload_file']) ? $_FILES['chat_upload_file'] : false;
$text_messege = !empty($text_messege) ? $text_messege : false;
if($file && $text_messege){
//both
}else if($file){
//file only
}else if($text_messege){
//message only
}else{
//nither
}
So you only check empty 2x instead of 4x.
Original and Issues
the condition i want to implement is: when user send both file and text message it will return an error.
You have to (or should) consider 4 possibility (2^2) each variable can have 2 states, well call them on and off
File | Message
on on
on off
off on
off off
In your original, you are only considering 2 of them
File | Message
on on
on off
Because:
if($_FILES['chat_upload_file']){
if($text_messege){
//file: on message: on
}else{
//file: on message: off
}
}
Does not consider what happens when no file is submitted. And if it's an error to send a File and a Message, then it must be ok to send just a message and no file. Otherwise, you would never be able to send a message (you cant send on with a file, and you cant send one without). Maybe there is stuff below this? I have no way to know that, though.
It makes little sense to add an else to the outer IF, because you will probably wind up duplicating stuff.
if($_FILES['chat_upload_file']){
if($text_messege){
//file: on message: on
}else{
//file: on message: off
}
}else{
if($text_messege){
//file: off message: on
}else{
//file: off message: off
}
}
And it's a lot cleaner to have 1 control block with 4 conditions, then 3 control blocks with 6 conditions.
It's implied by what you want to do that:
it's fine to have just a file
it's fine to have just a message (you don't consider it)
it's not fine to have both a file and a message
who knows with neither (you don't consider it)
hope that makes sense.

Redirection with $_SERVER["PHP_SELF"] and using header()

is it possible to do this , im trying to validate a form then, it will redirect using header() if TRUE.. but it seems not to be working? or my method is completely wrong ?
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$email = $_POST["clientEmail"];
if ($email != $sentEmailClients) {
echo 'Please enter a valid email';
} else {
$newURL = "http://www.myurl.com";
header('Location: ' . $newURL);
}
}
Give us more details about what actually happens when you run your code. You're likely facing one of the following problems:
You're using header() after you've already sent output to the browser. Headers must be sent before any other output. Check out the docs. If you change that line with die('redirecting') and that text shows up, then this is your problem.
Request method is not POST. Add die($_SERVER['REQUEST_METHOD']). If something other than POST is printed, then this is your problem.
$_POST['clientEmail'] is not set, or is not equal to $email
$email is not what you expect (where does it come from?)
$sentEmailClients is not what you expect (where does it come from?)
Basically, "why doesn't it work?" is not a good question because it doesn't give us much info with which to help you. Be more specific about what is happening.
Show enough of your code that we understand the origin of the variables you use.
Hi It seems that Your outermost if condition is not working thats why your header function is not working i just tried this and it woks fine. That means either your first if condition is false or either second if condition becomes true every time just try to echo your values before checking them.
<?php
if (1) {
$email = $_POST["clientEmail"];
if (0) {
echo 'Please enter a valid email';
} else {
$newURL = "http://www.google.com";
header('Location: ' . $newURL);
}
}
check this print_r($_SERVER["REQUEST_METHOD"]); is POST or not and
$email != $sentEmailClients true or false

Using if else statement based off of $_POST value to echo message

Pretty sure this is a quick and easy question but I have a form that on action POST goes to a confirmation page. I need a message to display on the confirmation page if the user selects county1 but if user selects county2, county3, or county4. However, when I setup the statement it's not working. Probably a syntax error or two on my part. Any help would be greatly appreciated.
A messy idea of what I think should work:
<?php $county=$_POST['County'];
if ($county="Polk") {
echo "Important message about your county"; }
else {
echo " "; // Or nothing at all
}
?>
But
<?php echo $_POST['County'] ?>
displays the name of the county so I know the submission is carrying through. Thoughts on why my above code wouldn't be working? If you could flag syntax errors or code placement that'd be much appreciated! Thank you!
Inside the if condition you should use two equal operators instead of one . try this code
<?php
$county = isset($_POST['County'])?$_POST['County']:"";
if ($county == "Polk") {
echo "Important message about your county";
}
else {
echo " "; // Or nothing at all
}
?>
Use double equal in your if statement for comparison
See the answer and read the comment to understand why you have to change it
if ($county="Polk") {
Single equal is assignment operator, it assign value
Change this line to this
if ($county=="Polk") {
So your whole code should look like this
$county=$_POST['County'];
if ($county == "Polk") {
echo "Important message about your county";
}
else {
echo " "; // Or nothing at all
}
Use double equal sign, double equal is comparison operator,
Here you are checking if the $county is equal to Polk or not,
Means you are comparing value

PHP Session Function Debug Assist

I know how to include external PHP pages and how to start sessions etc, but I think there is something messed up with my logic on what I am working on. Hoping someone could take a look...
I have an html page that is a form that pulls up a PHP view page with the info it sends to it. I wanted to put my function in an external page, along with using sessions, but I keep getting a syntax error.
When I send my form it goes to the following:
<?php
session_start();
include 'functs.php';
if ($_POST && !empty($_POST['name'])) {
$_SESSION['name'] = $_POST['name'];
$_SESSION['time'] = $_POST['time'];
confirmed();
}
else {
print unconfirmed();
}
?>
My external page with the functions is this:
<?php
function confirmed() {
echo "<head>";
echo "<title>Confirmation Page</title>";
echo '</head>";
echo "<body>";
PRINT <<<HERE
if (isset($_SESSION['name'])) {
echo 'Thank you, '.$_SESSION['name']. ' your reservation is confirmed for ' . $_SESSION['time'] ;
}
else {
echo 'There seems to have been an error processing your reservation. Please ensure that you have cookies enabled and try your request again' ;
}
HERE;
echo "</body></html>";
?>
The error I am getting is Parse error: syntax error, unexpected 'name' (T_STRING), expecting ',' or ';' in E:\Program Files\xampp\htdocs\cis\w2\functs.php on line 10. If I insert the function internally, it works, so I know its something with how I am formatting the include page.
It's pretty obvious via the syntax highlighting what is wrong here:
echo '</head>";
//-----------^
This line has the incorrect quote mark, thus you never terminate the string, and it keeps going.
Edit:
But that isn't the only problem. You also never close your function with a right curly bracket: }.
The main problem is that you have mismatched quotes on this line:
echo '</head>";
However, I have to say I'm confused as to why you have the HEREDOC. Surely you just need the if statement alone?

Need help deciphering PHP code

I am running through a jQuery Ajax tutorial here:
http://www.charlieperrins.com/2011/03/ajax-jquery-101/
Everything works perfectly but I have a question about this piece of code:
<?php if ($_POST['user']) : ?>
<?php
$user_id = $_POST['user'];
if (isset($db_data[$user_id])) {
$data = $db_data[$user_id];
} else {
echo 'Sorry, no user data matched your request - please try again';
die;
}
?>
I am most concerned with the very first line. What does that line do? I am trying to keep all the code in 1 set of php tags but I don't know how to do that. If I knew what the first line does, I might be able to figure it out. Any help is appreciated. I am trying to reverse engineer this to fit it into my app but can't do it without knowing what that top line does.
Thanks.
All this does is continues the if block until endif.
There is no endif, so nothing in this script runs unless there is data in $_POST['user'] that doesn't evaluate to false.
I would write this a bit differently:
<?php
if (isset($_POST['user'])) {
$user_id = $_POST['user'];
if (isset($db_data[$user_id])) {
$data = $db_data[$user_id];
} else {
echo 'Sorry, no user data matched your request - please try again';
die;
}
}
?>
The first line tests if the $_POST array has a key user, and that key contains a "truthy" (non-empty, among other things) value, indicating that a form was posted to this script. If no form data was posted, the rest of the script won't execute, such as if someone browsed directly to this PHP script without using the expected form to post to it. It is a technique often used when a form posts back to the same PHP script. Upon first arriving at the script, the $_POST will be empty. When the form is posted back to the same script, different actions can be taken when it contains values.
There need only be one <?php tag:
<?php
if ($_POST['user']) {
$user_id = $_POST['user'];
if (isset($db_data[$user_id])) {
$data = $db_data[$user_id];
} else {
echo 'Sorry, no user data matched your request - please try again';
die;
}
}
?>
This is Alternative syntax for control structures
<?php if ($_POST['user']) : ?> means if $_POST['user'] evaluates to true, execute the following code.
It can be compressed down to this:
<?php if ($_POST['user']) :
$user_id = $_POST['user'];
....
Also,
if ($_POST['user']) :
should be
if (isset($_POST['user']) && !empty(trim($_POST['user']))) :
That makes sure that $_POST['user'] has been set (generally $_POST contains variables from a form), and that it is not empty even with white-space removed.
See
Alternative syntax for control structures
$_POST
empty
trim
The if ($_POST['user']) line is saying this:
If the variable $_POST['user'] exists and is set to a non-false value.
The above condition fails if $_POST['user'] is 0, false, or '' (empty string).
It also isn't safely checking that value.
You are better off using:
if (isset($_POST['user'])) && $_POST['user'] != '')
This way no warning is output when PHP has display_errors and notices turned on.

Categories