PHP Contact Form Help

Soldato
Joined
19 Dec 2007
Posts
2,663
Location
Newton Aycliffe
Code:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sending...</title>
</head>
<?php 
$to = "[email protected]"; 
$subject = "Contact recieved from Jonnyisms";
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ; 
$message = $_REQUEST['message']; 
$headers = "From: $email"; 
$sent = mail($to, $subject, $message, $headers) ; 
if($sent) 
{print "Thank you $name, Your mail was sent successfully."; }
else 
{print "We encountered an error sending your mail, please click back on your browser."; }
?>
<body>
</body>
</html>

I have the above as a php page which processes the contact form on my website, my questions are:

1. how can i get this page to automatically forward the user back onto a page in my website.

2. how can i get the email to display the name of the sender as well, at the moment I have it collecting the user's name but it is only used in a line to thank them for sending.
 
Code:
1. how can i get this page to automatically forward the user back onto a page in my website.

2. how can i get the email to display the name of the sender as well, at the moment I have it collecting the user's name but it is only used in a line to thank them for sending.[/quote]


for redirecting you have 2 options, php or javascript. php will redirect straight after the form is submitted so if you want to display the success message you will need to put it on the page the user is sent to. 

however with javascript you could add a timer so you could get php to print the success message and add that they are about to be redirected then have javascript count to whatever you want, like say 10 seconds and redirect

code for php-  this would replace this exact text, [B]print "Thank you $name, Your mail was sent successfully.";  [/B]
[php]
header('Location: redirectPage.html');
[/php]code for javascript with timer

[code]

<script type="text/javascript">
<!-- 
//remove the line below if you don't want a timer
t=setTimeout('redirect()',500)

function redirect()
{
window.location = "redirectPage.html"}
//-->
</script>
hope this helps
 
Last edited:
Thanks, that's excellent, I have used the java script there so I can leave the success message as it is. :)

Just need to sort the name sending now, as if I get the message from someone with a random email I wont know what they're called!
 
Thanks, that's excellent, I have used the java script there so I can leave the success message as it is. :)

Just need to sort the name sending now, as if I get the message from someone with a random email I wont know what they're called!

ah silly me i forgot to answer that one

so this is the code you have

$sent = mail($to, $subject, $message, $headers) ;

each step doesn't actually matter on the name, just as long as what you want is in the right place, so where it says $to, this could be changed to $myaddress, just as long as the variable at the top is changed to match it is ok. so you know the structure of an email that gets sent, the first part is who it goes to, then subject, then message, then headers, each separated by a comma.

what you want to do is chance the message (the third part of the mail function), which there is 2 options of doing it. either changing the variable $message when it first is defined as $_REGUEST blah blah blah, to also including a bit of text, or just include the name in the third section of the email.

here is the 2 options

PHP:
//option 1:

$name = $_REQUEST['name'] ;
$message = "This email has been sent from: " . $name . "<br /><br />" . $_REQUEST['message'];


//option 2: not sure if this will actually work, fairly certain it will but give it a try and see

$sent = mail($to, $subject,"This email has been sent from: " . $name . "<br /><br />" . $message, $headers) ;
sorry for the long post
 
Last edited:
I have used both solutions there, so not it tells me in my message (using option 1) and in the from field using Xtrm2Matt's solution.

thanks mucho!
 
Back
Top Bottom