Log in

View Full Version : Formmail: How do I redirect to HTML page?



SharezS
08-19-2006, 11:41 AM
Hello,

How can I direct users to a specific URL rather than have them see a message like "All fields are required!" or "Your email has been sent." What is the correct syntax? I made a formmail with the following PHP script:



<?php

// field validation
if ($email=="" || $message=="" || $name==""|| $subject=="")

{
print ("All fields are required! Please go back and try again.");
}

else {

// email validation
if(!eregi('^([._a-z0-9-]+[._a-z0-9-]*)@(([a-z0-9-]+\.)*([a-z0-9-]+)(\.[a-z]{2,3})?)$', $email)) {
print ("Your email address does not appear to be valid. Please go back and try again.");
exit;
}

// send email
$to = "info@mysite.com";
$msg = "From: $name\n\n";
$msg .="Email: $email\n\n";
$msg .="Subject: $subject\n\n\n\n";
$msg .= "$message\n\n";

mail($to, $subject, $msg, "From: $name\nReply-To: $email\n");
print ("Thank you $name, your email has been sent.");

}
?>



Thank you. All help will be appreciated.

Sharez

costas
08-19-2006, 11:54 AM
Simply by using the "header()" function. E.g.
if ($email==NULL || $message==NULL || $name==NULL|| $subject==NULL)

{
header("Location: somepage.php");
}

else ............

If you want a complete guide for php and mysql, a good book to buy is Julie's C. Meloni "Learn PHP, MySQL and Apache"(or something like that, I have it in a Greek version, because I'm from Greece so I don't know excactly the name in English).

costas
08-19-2006, 11:55 AM
Hope I helped.

SharezS
08-19-2006, 12:34 PM
Thank you very much Costas. This is exactly what I needed.



Sharez

blm126
08-19-2006, 03:25 PM
Simply by using the "header()" function. E.g.
if ($email==NULL || $message==NULL || $name==NULL|| $subject==NULL)

{
header("Location: somepage.php");
}

else ............


THat code should not be relied on as is. You need an exit statment to ensure that the content is not shown if the browser does not redirecTry this.


if ($email==NULL || $message==NULL || $name==NULL|| $subject==NULL)

{
header("Location: somepage.php");
exit;
}

else ............

mwinter
08-19-2006, 04:32 PM
THat code should not be relied on as is.

Particularly because the Location header requires an absolute URL.



You need an exit statment to ensure that the content is not shown if the browser does not redirect.

And if the browser doesn't redirect, a note should be included in the response containing a link to the real destination (as the HTTP/1.1 specification recommends).

Mike