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]