Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
3  
Silly question... but should it be "No" with a capital like the "Yes"? – mjayt Apr 2 at 20:08
I wish it was that simple, but it's actually &var=no. This is coming from a seperate service. – user1272433 Apr 2 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 .. – dbf Apr 2 at 20:37

2 Answers

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.

share|improve this answer
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. – user1272433 Apr 2 at 20:21

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

share|improve this answer
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. – user1272433 Apr 2 at 20:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.