I have a simple "contact me" form. Is made in html (of course), validate in Javascript than I use Ajax request and send all of it to a php file, where the form should be validated again and than send it. (i know just a bit of php so i hope is like that!)
The "success" or "not submit" message are in the html and displayed by ajax/js. So my question is easy: How can i send the body to my personal email including the php validation? I show you my code:
Js/ajax:
myForm.submitForm = function(){
var formData = {
name: myForm.name.val(),
email: myForm.email.val(),
text: myForm.text.val(),
};
$.ajax({
url: 'contact.php',
method: 'post',
//dataType: 'json';
success: myForm.submitSuccess,
error: myForm.submitError
})
};
myForm.submitSuccess = function(data, textStatus, jqXHR){
console.log(data, textStatus, jqXHR);
myForm.succMess.html(data.message);
myForm.succMess.show();
};
myForm.errorSuccess = function(jqXHR, textStatus, errorThrown){
console.log(jqXHR, textStatus, errorThrown);
myForm.errorMess.html(textStatus);
myForm.errorMess.show();
};
Php:
<?php
$nome = $_POST['name'];
$email = $_POST['email'];
$text = nl2br($_POST['text']);
//mail() I guess i should use this one but how?!
//check if inputs are empty
if(!empty($nome) and !empty($email) and !empty($text)){
$mail_to = "[email protected]";
$mail_from = $email;
$mail_subject = "Email sent by $nome";
$mail_body = "$nome: $email. $text";
?>
Any help is really appreciated :)
mail()function. If you want to send the$mail_body, just usemail($mail_to, $mail_subject, $mail_body, $headers);mail();would go under$mail_body = "$nome: $email. $text";and not under$text = nl2br($_POST['text']);mail($mail_to, $mail_subject, $mail_body, $headers);after$mail_bodyand that's all?&&instead ofand