So I've been working on creating a contact form. No matter what I try, I can't get it to work. I've read different blogs, how to's, etc. I would love some help. Note: when it says [email protected], I've input my own email.
HTML:
<div class="col-lg-6 offset-lg-1">
<form action="form-data/formdata.php" class="form-widget form-control-op-02">
<div class="field-wrp">
<input type="hidden" name="to"value="[email protected]">
I won't bore you with all the details of the form fields i.e. name, phone number, email, etc.
PHP:
if (isset($_POST) && sizeof($_POST) > 0) {
$to = $_POST['to']['val']; // <=== Set static email here.
if (isset($_POST['formtype'])) {
unset($_POST['formtype']);
}
if (isset($_POST['to'])) {
unset($_POST['to']);
}
$email_address = $_POST['email']['val'];
$email_subject = "Form submitted by: ".$_POST['name']['val'];
$email_body = "You have received a new message. <br/>".
"Here are the details: <br/><br/>";
foreach ($_POST as $key => $value) {
$email_body .= "<strong>" . $value['label'] . ": </strong> " . $value['val'] . "<br/><br/>";
}
$headers = "From:<$email_address>\n";
$headers.= "Content-Type:text/html; charset=UTF-8";
if($email_address != "") {
mail($to,$email_subject,$email_body,$headers);
return true;
}
}
?>
$_POST['to']['val']if the you named the inputname="to"? It should be just be$_POST['to'](and probably the same for the other inputs as well). It's also not good to have the "to" address in the form. People can change that and use your form to spam other people. Hard code the "to" address in your PHP instead.