Page 1 of 1

PHP Re-Direct on Condition

Posted: Fri Apr 10, 2009 4:25 pm
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

Re: PHP Re-Direct on Condition

Posted: Fri Apr 10, 2009 4:54 pm
by requinix
Any reason you can't use header to do the redirection instead?
And what did you try to change the <meta> to?

Re: PHP Re-Direct on Condition

Posted: Fri Apr 10, 2009 5:26 pm
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

Re: PHP Re-Direct on Condition

Posted: Fri Apr 10, 2009 5:33 pm
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???

Re: PHP Re-Direct on Condition

Posted: Fri Apr 10, 2009 6:07 pm
by requinix
hankhendrix wrote:The meta I tried is:
...
Missing some quotes in the code.

Re: PHP Re-Direct on Condition

Posted: Fri Apr 10, 2009 6:11 pm
by hankhendrix
How do you mean?

Re: PHP Re-Direct on Condition

Posted: Fri Apr 10, 2009 6:18 pm
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.

Re: PHP Re-Direct on Condition

Posted: Fri Apr 10, 2009 6:46 pm
by hankhendrix
Yeh I tried inserting ' instead of " but that didn't make a difference. Am I able to just insert the full URL?

Re: PHP Re-Direct on Condition

Posted: Fri Apr 10, 2009 7:26 pm
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\">";