PHP Re-Direct on Condition

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hankhendrix
Forum Newbie
Posts: 5
Joined: Fri Apr 10, 2009 4:15 pm

PHP Re-Direct on Condition

Post by hankhendrix »

Hi there,

This is my first post and I am a PHP Noob so go easy if the answer to this article is in the depths of the database somewhere.

Basically I have an email form that is processed via a php file. When the user successfully submits their details I want them to be re-directed to a URL.
Currently the script I am using only re-directs to "URL=ok.htm\" but I want to re-direct with an absolute path URL because the actual site is on another server and not in that relative directory.

Here's the code:

Code: Select all

 
<?php
 
// get posted data into local variables
$EmailFrom = Trim(stripslashes($_POST['EmailFrom'])); 
$EmailTo = "xxxxxxxxx@xxxx.xxx";
$Subject = "hi";
$hello = Trim(stripslashes($_POST['hello'])); 
 
// validation
$validationOK=true;
if (Trim($EmailFrom)=="") $validationOK=false;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}
 
// prepare email body text
$Body = "";
$Body .= "hello: ";
$Body .= $hello;
$Body .= "\n";
 
// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
 
// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
 
I have tried inserting "http://www.xxxx.com/xxx/ok.htm" on line 29, but to no avail.

I know this is probably a simple syntax error or I am missing something, but I can't work it out!


Cheers

Hank
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP Re-Direct on Condition

Post by requinix »

Any reason you can't use header to do the redirection instead?
And what did you try to change the <meta> to?
hankhendrix
Forum Newbie
Posts: 5
Joined: Fri Apr 10, 2009 4:15 pm

Re: PHP Re-Direct on Condition

Post by hankhendrix »

I have tried using "header" but I might not have implemented it properly.

The meta I tried is:

Code: Select all

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=http://www.xxxxxxxx.com/xxxxxx/thankyou.asp">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URLhttp://=www.xxxxxxxx.com/xxxxxx/error.asp">";
}
I am aware that I have used forward and backward slashes in the meta line and wonder if that affects it.

Cheers

Hank
hankhendrix
Forum Newbie
Posts: 5
Joined: Fri Apr 10, 2009 4:15 pm

Re: PHP Re-Direct on Condition

Post by hankhendrix »

BTW, I'm aware that to use header it has to have no code before it (is that right?) and I have got all the code before the if(condition) actually processing the form. So I wouldn't be able to place header before the code otherwise it wouldn't be processed (??).

I could actually leave the re-direct as:

Code: Select all

if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\">";
}
...and then change the file destination to a php file with the header function in it which would automatically re-direct back to the desired URL???
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP Re-Direct on Condition

Post by requinix »

hankhendrix wrote:The meta I tried is:
...
Missing some quotes in the code.
hankhendrix
Forum Newbie
Posts: 5
Joined: Fri Apr 10, 2009 4:15 pm

Re: PHP Re-Direct on Condition

Post by hankhendrix »

How do you mean?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP Re-Direct on Condition

Post by requinix »

Maybe I'm not exactly right.

Code: Select all

print "<meta http-equiv=\"refresh\" content=\"0;URL=http://www.xxxxxxxx.com/xxxxxx/thankyou.asp">";
 
print "<meta http-equiv=\"refresh\" content=\"0;URLhttp://=www.xxxxxxxx.com/xxxxxx/error.asp">";
 
 
Looks a little screwy.
hankhendrix
Forum Newbie
Posts: 5
Joined: Fri Apr 10, 2009 4:15 pm

Re: PHP Re-Direct on Condition

Post by hankhendrix »

Yeh I tried inserting ' instead of " but that didn't make a difference. Am I able to just insert the full URL?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP Re-Direct on Condition

Post by requinix »

hankhendrix wrote:Am I able to just insert the full URL?
You should, yeah.

Code: Select all

print "<meta http-equiv=\"refresh\" content=\"0;URL=http://www.xxxxxxxx.com/xxxxxx/thankyou.asp\">";
 
print "<meta http-equiv=\"refresh\" content=\"0;URL=http://www.xxxxxxxx.com/xxxxxx/error.asp\">";
Post Reply