PHP Contact Form Help

Hello,

Bit of a novice with PHP but I am trying to build a contact form for my website which submit’s entries to my email address.

I have built the html form and have used the below php code which seems to work.

At the moment, if the user fails to complete a required field, a new blank page is shown with the error message. However I would like the error to display above the contact form rather than on a blank page. How would I go about this? Would it be a case of adding the html code from the contact form page, into the php code?

Thanks

[php]

<?php $myemail = "[email protected]"; $yourname = check_input($_POST['yourname'], "Please enter your full name"); $email = check_input($_POST['email']),, "Please enter a valid email address"); $how_find = check_input($_POST['how']); $comments = check_input($_POST['comments'], "Please add a comment"); if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) { show_error("E-mail address not valid"); } $message = " Your contact form has been submitted by: Name: $yourname E-mail: $email How did he/she find it? $how_find Comments: $comments"; mail($myemail, $subject, $message); header('Location: confirmation.html'); exit(); function check_input($data, $problem='') { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); if ($problem && strlen($data) == 0) { show_error($problem); } return $data; } function show_error($myError) { ?>
<html>
<body>

<b>Please correct the following error:</b><br />
<?php echo $myError; ?>

</body>
</html>
<?php exit(); } ?>[/php]

Hey Mike,

What you have there is just the basic part of the PHP form… You need to add AJAX or JAVASCRIPT to do what you are looking for. Look up on Google because I can’t post a link yet… PHP Javacript contact form The first Link: http://www.html-form-guide.com/contact-form/php-contact-form-tutorial.html is a very good tutorial to show you how to do what your looking for.

You don’t need js or ajax to do error displays - why would you tell him that? You wouldn’t use ajax anyways since there’s no database calls, maybe jquery if you wanted to fancy it up some.

All that’s needed is to to make a couple of changes. Get rif of those useless functions, they aren’t really doing anything to detect problems.

2nd - just do simple if statements

if(empty($_POST[‘yourname’])) {
$error[‘name’] = “Name is Required”;
} else {
$yourname = strip_tags($_POST[‘yourname’];
}

3rd - Do another if statement looking for $error

if(!$error) {
// do mail stuff here
} // no else

4th - display the error messages where they’re needed.

You can definitely do this with javascript, but its a lot more complicated.

Thank you for the replies and tips

Sorry stupid question but where would I need to place the if statements in the code above? Also you said to display the error messages where needed, how would I go about this? Sorry like I said i’m a novice to PHP.

I’ve managed to edit the code to display the error messages after validation now

Can anyone help with the code below to send the email? At the moment it sends an email with a subject of (New Contact Us Form) and the data from the ‘comments’ field. How would i need to to edit the code below to include other fields on the email, i.e. telephone number, name.

[php][/php]

function ProcessForm($values)
{
mail(‘[email protected]’, ‘New Contact Us Form’, $values[‘comments’], “From: “{$values[‘name’]}” <{$values[‘email’]}>”);

Sponsor our Newsletter | Privacy Policy | Terms of Service