我正在使用Jquery Ajax和PHP在html中开发一个常规表单。
当我通过php函数邮件发送电子邮件时,我收到下一封空字段的电子邮件,我不知道原因:
From:
E-Mail:
Message:
我使用的代码:
-PHP(sendMail.php):
<?php
if($_POST){
$name = $_POST['form_name'];
$email = $_POST['form_email'];
$content = $_POST['form_content'];
$from = 'Koke Contact Form';
$to = 'wheretosendit@gmail.com';
$subject = 'Someone is contacting';
$body = "From: $name\n E-Mail: $email\n Message:\n $content";
//send email
if (mail ($to, $subject, $body)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
?>
-HTML
<form method = "post" id = "message" action = "sendMail.php">
<div class="row">
<div class = "yourinfo col-md-6">
<div class = "form-group" id = "nombre">
<label for = "input-name"> Tu nombre: </label>
<input id= "form-name" type= "text" class= "form-control gborder" name = "form-name">
</div>
<div class = "form-group " id = "email">
<label for = "input-email"> Tu email: </label>
<input id = "form-email" type= "email" class= "form-control gborder" name = "form-email">
</div>
</div>
<div class = "form-group col-md-6" id = "comment">
<label for = "input-content"> Mensaje: </label>
<textarea id = "form-content" class="form-control gborder" rows = "5" name = "form-content"></textarea>
</div>
</div>
<div class="form-group">
<div class = "text-center">
<input id="submit" name="submit" type="submit" value="ENVIAR" class="btn btn-primary">
</div>
</div>
</form>
-JQUERY
$('#message').submit(function() {
if($('#form-name').val() !== "" && $('#form-email').val() !== "" && $('#form-content').val() !== "") {
var data = {
name: $('#form-name').val(),
email: $('#form-email').val(),
content: $('#form-content').val()
};
$.ajax({
type: 'POST',
url: 'sendMail.php',
data: data,
success: function () {
$("html, body").animate({ scrollTop: 0 }, "slow");
$('#form-content').val("");
$('#form-name').val("");
$('#form-email').val("");
}
});
} return false;
});
正如我所说,我收到了电子邮件,但发件人,电子邮件和邮件字段为空。我从2天前就开始与之斗争了......我不知道如何解决这个问题。
感谢您的帮助!
答案 0 :(得分:2)
您从js array发送
var data = {
name: $('#form-name').val(),
email: $('#form-email').val(),
content: $('#form-content').val()
};
但是在php中你试图访问其他字段
$name = $_POST['form_name'];
$email = $_POST['form_email'];
$content = $_POST['form_content'];
Ajax name
和php form_name
,但也必须是name
。