I'm pretty new to using php and I'm currently trying to set up a personal site. I've got a contact form in the page at the bottom that I can't seem to get to work and send myself an email of the message the client writes. The form has the following html:
<form method="post">
<div class="form-group">
<label for="name">Your name:</label>
<input type="text" id="name" class="form-control" placeholder="Your name" />
</div>
<div class="form-group">
<label for="email">Your email:</label>
<input type="email" id="email" class="form-control" placeholder="Your email"/>
</div>
<div class="form-group">
<label for="comment">Message</label>
<textarea class="form-control" id="comment"></textarea>
</div>
<input type="button" class="btn btn-warning" value="submit" id="sendEmail"/>
</form>
and I'm using the to following JavaScript to put the form into a php file and send out an email to myself. Here in the JavaScript:
<script>
$(".contentContainer").css("min-height",$(window).height());
$("result").hide();
$("#sendEmail").click(function(){
var name = $("#name").val();
var email = $("#email").val();
var comment = $("#comment").val();
$.post("validate.php",
{postname:name,postemail:email,postcomment:comment},
function(data){
$("#result").html(data).fadeIn();
});
});
</script>
and here is the php file that I'm using:
<?php
$error = NULL;
if (!$_POST['postname']) {
$error="<br />Please enter your name";
}
if (!$_POST['postemail']) {
$error.="<br />Please enter your email address";
}
if (!$_POST['postcomment']) {
$error.="<br />Please enter a comment";
}
if ($_POST['postemail']!="" AND !filter_var($_POST['postemail'], FILTER_VALIDATE_EMAIL)) {
$error.="<br />Please enter a valid email address";
}
if($error !=NULL){
$result='<div class="alert alert-danger"><strong>There were error(s)
in your form:</strong>'.$error.'</div>';
} else{
if (mail("PERSONAL EMAIL", "Comment from website", "Name: ".$_POST['postname ']."
Email: ".$_POST['postemail']."
Comment: ".$_POST['postcomment'])) {
//echo $_POST['postname']
$result='thank you!';
} else{
$result='There was an error sending your message! Try again later!';
}
}
?>
A couple of things I've noticed: when I replace the 'PERSONAL EMAIL', I don't receive an email when I test out the contact form on my website. However, when I uncomment the echo $_POST['postname'] right after the mail statement (I interpreted it as mail=true so mail sent) then the variable echos back to my website. However, the $result variable doesn't appear in the #result div like I intend. What am I dong wrong? Is there an error I've made that I need to know for future reference? This concept between html <-> php is still kind of new to me!