Forum Moderators: coopster

Message Too Old, No Replies

Will this PHP code work?

         

kslnor

10:22 pm on Mar 31, 2007 (gmt 0)

10+ Year Member



I want to have a 'contact us' form, via PHP, but I'm afraid I only have basic knowledge. So, I have the below code, which works. When the form is submitted, the user gets a plain white page that has the statement "Your mail was sent successfully" (or "We encountered an error sending your mail"). How can I change it so the user gets a page that has the layout of my site - or at the very least, how can I add a link to go back to my site without having to hit the browser's back button? Any other modifications would be greatly appreciated - just want to keep it simple. Thanks

HTML:

<p class="form1">
<form method="post" action="contact.php">
Name: &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input name="name" type="text"><br>
Company: &nbsp&nbsp&nbsp<input name="company" type="text"><br>
Email: &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input name="email" type="text"><br><br>
Message:<br>
<textarea name="message" rows="15" cols="40"></textarea><br><br>
<input type="submit" value="Send message!">
</form>
</p>

PHP:

<?php
$to = "contact@example.com";
$subject = "Contact Us";
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers) ;
if($sent)
{print "Your mail was sent successfully"; }
else
{print "We encountered an error sending your mail"; }
?>

[edited by: eelixduppy at 2:17 am (utc) on April 2, 2007]
[edit reason] exemplified domain [/edit]

cameraman

10:50 pm on Mar 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can intersperse html and php. So after your mail() line:
.
.
$sent = mail($to, $subject, $message, $headers) ;
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="yourstylesheet.css" />
</head>
<body>
<?php
if($sent) {
?>
<p>Congratulations, your email has been sent.</p>
<p>Click <a href="index.html">here</a> to return to the home page</p>
<?php
} // EndIf mail was sent
else {
?>
<p>We encountered an error sending your mail.</p>
<p>Click <a href="contact.php">here</a> to try again</p>
<p>or <a href="index.html">here</a> to return to the home page</p>
<?php
} // EndElse mail not sent
?>
</body>
</html>

kslnor

2:44 pm on Apr 1, 2007 (gmt 0)

10+ Year Member



Thanks, cameraman. Works great. Once the "congrats...click here..." page appears, do you have any idea how I can have it redirect to the homepage (so my visitors don't have to "click here"). Meanwhile, I'm trying to figure it out for myself... Thanks.

cameraman

5:58 pm on Apr 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can use a meta refresh tag, place in the header:
<META HTTP-EQUIV="Refresh" content="5; URL=http://example.com/index.html">
the 5 is the number of seconds to wait before it redirects. You might want to leave the link there, change the wording to something like 'if your browser doesn't support redirection, click here to return to the home page'.