View Full Version : PHP Contact Form

11-09-2012, 01:02 PM

I am having some problems with PHP code to send my form submissions to an email address: I have the following code at present,

mail($email, 'New Contact Us Form', $values['text'], "From: \"{$values['name']}\" <{$values['email']}>");

This produces an email but no data is shown on it. Can anyone help with how I need to adjust the code so that all fields from the contact form display on the resulting email.

The values for the fields I have in the form are: name, phone, email, comments.

Also, after the user submits my form, if there are uncompleted fields, the page is re-loaded with the form error messages. At the moment, the page re-loads to the top, however I would like it to re-load at the same position that the contact form sits. If it's possible, what code would I need to use?

Many Thanks

11-10-2012, 12:35 AM
What is in $values['text']? I'd bet it's blank. Find out by inserting <icode>echo $values['text'];</icode> before the mail() call.

You can't really control page position like you want. You could use in-page anchor tags (google it), but that will only approximate what position the page was in when the form was submitted. (which may be good enough for you)

11-10-2012, 08:37 PM
Thanks for the reply. I have used a page anchor and also added it to the form action code which seems to work.

I'm having some problems with the redirect to a confirmation page. I have tried using the header code below but this doesn't redirect?


header( 'Location: http://www.yoursite.com/new_page.html' ) ;


I've also used the meta code below and this works however there is a slight delay before the page redirects even though content is set at 0? Any ideas why this is?

$url = 'confirmation.html';
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';

11-11-2012, 03:47 AM
header() works fine, I use it all the time, but you may have already produced output before calling header(), which produces errors/warnings, which you may not be seeing because you may have error reporting turned off. Make sure error reporting is turned on while you are writing code.

The <meta refresh> tag has a delay because the page has to fully load to the browser before it fires off. header() doesn't load anything to the browser.

11-13-2012, 05:11 PM
Thanks for your help, I've managed to get the header code working now