Forum Moderators: coopster

Message Too Old, No Replies

condition based redirection

         

modouno

4:44 pm on Oct 3, 2006 (gmt 0)

10+ Year Member



Hi everyone, I have just created my first php form and everything is working great. Now that the form is functional, I was thinking a little error checking would be good. My form POST to a new php page which then processes the answers submited and emails them to me. What i want to do is have a conditional redirect that will read the the submitted feild, then if the feild was left blank, kill the script and send the visitor to the first page with a new error message asking them to fill out all form feilds.

All redirect info in php I have been able to find says it must be in the head, before anything else. I was thinking it would be better in my situation to do something like (if possible)

if ($x="" ){
redirect http:www.example.com;}

Now i'm sure its not as simple as that, but is there something like that I can use?

Can anyone point me in the right direction, or help me out with this? Thanks alot

sned

5:02 pm on Oct 3, 2006 (gmt 0)

10+ Year Member



You can use the header [us2.php.net] function:

if($x == ""){
header("Location: some_page.php");
}

-sned

eelixduppy

5:02 pm on Oct 3, 2006 (gmt 0)



Welcome to WebmasterWorld!

something like this:


if([url=http://us2.php.net/manual/en/function.empty.php]empty[/url]($_POST['field'])) {
[url=http://us2.php.net/manual/en/function.header.php]header[/url]("Location: form.html");
[url=http://us2.php.net/manual/en/function.exit.php]exit[/url]();
}

good luck!

modouno

5:14 pm on Oct 3, 2006 (gmt 0)

10+ Year Member



Thanks alot sned and eelixduppy.

I'll give it a shot.

modouno

5:44 pm on Oct 3, 2006 (gmt 0)

10+ Year Member



Ok, I tried it out and I am appearing to have a problem.

The script is effectivly stopped if the feild is left blank, but the redirection is not happening.

Everything seems to be setup correctly, any ideas why the above script would not redirect? Thanks

sned

6:02 pm on Oct 3, 2006 (gmt 0)

10+ Year Member



The redirect needs to happen before any output is sent to the browser.

If you have output before the redirect, you should get an error something like: Cannot modify header information - headers already sent by (output started at somefile.php) in anotherfile.php

-sned

modouno

6:07 pm on Oct 3, 2006 (gmt 0)

10+ Year Member



thanks for replying sned, I'm not getting any error messages, the page simply stops loading if the field is left blank. This is the code I am using. It is located on line 1

<?php if(empty($_POST['email'])) {
header("Location: http://www.example.com");
exit();
}

?>

sned

6:15 pm on Oct 3, 2006 (gmt 0)

10+ Year Member



Hmm, that sounds like the if() condition isn't catching the empty field. You could try something like:

<?php
if(empty($_POST['email'])) {
echo "field is empty";
//header("Location: http://www.example.com");
exit();
}
?>

to test the if condition. It might be an empty [us2.php.net] vs isset [us2.php.net] thing.

modouno

6:34 pm on Oct 3, 2006 (gmt 0)

10+ Year Member



no luck,

"the field is empty"

printed just fine.

something weird.....

After the test, I pasted the old code back in and everything works.... I don't get it, but everything is working fine now.

Thanks alot sned