0

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!

3
  • 3
    Silly question... but should it be "No" with a capital like the "Yes"? Commented Apr 2, 2013 at 20:08
  • I wish it was that simple, but it's actually &var=no. This is coming from a seperate service. Commented Apr 2, 2013 at 20:18
  • To little information on how this is being put together, if a redirect works in the else if ($var == 'no') { part, then probably $errormsg is not in the same scope or its being overwritten somewhere with an empty value .. Commented Apr 2, 2013 at 20:37

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The thing is, when I place "header( 'Location: google.com' ); exit;" in the $var == 'no' statement it redirects me. So I must be doing something wrong with $errormsg.
0

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

1 Comment

That didn't do it, but this is on a WordPress site and I moved the PHP out of the header and onto the page template and it did work...for some reason when I was first testing I couldn't get the redirection to work unless it was in the header. Now it seems to be, thanks for everyone's help, I appreciate it, but I seem to have solved it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.