willH, not very sure what you want help with. Do you mean you want to add in the email code into the
other code? If so, do it this way:
[php]
<?php
$titleErr = $nameErr = $emailErr = $commentErr = "";
$title = $name = $email = $comment = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["title"])) {
$titleErr = "You must enter Title!";
} else {
$title = test_input($_POST["title"]);
}
if (empty($_POST["name"])) {
$nameErr = "Must enter the Name!";
} else {
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Must enter the name Email!";
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["comment"])) {
$commentErr = "Must enter the name comment";
} else {
$comment = test_input($_POST["comment"]);
if (!preg_match("/^[a-zA-Z ]*$/",$comment)) {
$commentErr = "Only letters and white space allowed";
}
}
}
// Check error messages, if none, then send email...
if ($titleErr=="" AND $nameErr=="" AND $emailErr=="" AND $commentErr=="") {
$to = "@my email";
$from = $_POST['email'];
$title = $_POST['title'];
$name = $_POST['name'];
$subject = "Info from";
$subject2 = "Copy of your form submission";
$comment = $title . " " . $name . " " . "\n\n" . $_POST['comment'];
$comment2 = "Here is a copy of your comment " . $name . "\n\n" . $_POST['comment'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$comment,$headers);
mail($from,$subject2,$comment2,$headers2);
echo
header("Location: http://my web/thanks.html");
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
Write us what you need to know
* required field.
Title:
* <?php echo $titleErr;?> / Mr, Ms, … /
Name:
* <?php echo $nameErr;?> / First Name and Last Name /
E-mail:
* <?php echo $emailErr;?>
Comment: *
<?php echo $comment;?>
<?php echo $commentErr;?>
[/php]
There are better ways to do this project. One would be to just use one variable for the error messages.
You can concatenate the errors instead of using several variables for them. This means only one variable
and one check before sending the email. But, first let's get your code working.
The change I made was to check for the errors instead of checking for the submit button before sending
the email. You do not want to send the email if there is an error message. Hope that makes sense!
There has been a lot of discussion about security on this type of site. It has be decided that you should
not use an address inside of the form’s action function if it posts to itself. Therefore, you should change
the line: <form method=“post” action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
to this instead: On all new server’s this is handled correctly and it does
prevent hackers from seeing your page and folder layout if they grab your session info.
Oh, also, please place your code next time between the tags. Just press the PHP button above
your editor area and place the code inside those tags. It helps us copy them into our editors. Thanks!