HTML:
<form id="main-contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="row wow fadeInUp" data-wow-duration="1000ms" data-wow-delay="300ms">
<div class="col-sm-6">
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Name" required="required">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input type="email" name="email" class="form-control" placeholder="Email Address" required="required">
</div>
</div>
</div>
<div class="form-group">
<input type="tel" name="subject" class="form-control" placeholder="Phone" required="required">
</div>
<div class="form-group">
<textarea name="message" id="message" class="form-control" rows="4" placeholder="Enter your message" required="required"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn-submit">Send Now</button>
</div>
</form>
PHP:
<?php
$mail='kenhaki@gmail.com';
$nombre = $_REQUEST['name'];
$telefono = $_REQUEST['subject'];
$email = $_REQUEST['email'];
$mensaje = $_REQUEST['message'];
$headers = 'From: ccontacto@nhtt.com.mx' . "\r\n" .
'Reply-To: cContacto@nhtt.com.mx' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$thank="gracias.html";
$message = "
nombre15: $nombre
telefono: $telefono
email: $email
mensaje: $mensaje";
if (mail($mail,"Formulario de Contacto Nuevo",$message,$headers))
Header ("Location: $thank");
?>
JS:
// Contact form
var form = $('#main-contact-form');
form.submit(function(event){
event.preventDefault();
var form_status = $('<div class="form_status"></div>');
$.ajax({
url: $(this).attr('action'),
beforeSend: function(){
form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Email is sending...</p>').fadeIn() );
}
}).done(function(data){
form_status.html('<p class="text-success">Thank you for contact us. As early as possible we will contact you</p>').delay(3000).fadeOut();
});
});
我是一个初学者,我一直在搜索类似的问题,但没有找到为什么它无法从html表单获取字段的解释。 我尝试使用$ _REQUEST和$ _POST,但是没有任何作用。我在同一站点的另一部分使用了相同的表单,并且可以正常工作,也许在这里JS会以某种方式阻止它?
提前感谢您的耐心等候。
答案 0 :(得分:1)
您正在用event.preventDefault()
覆盖默认的表单操作。然后,您的所有AJAX调用都在做,正在请求sendemail.php
的内容-它没有向其发送任何数据!
尝试将您的JS代码更改为此:
// Contact form
var form = $('#main-contact-form');
form.submit(function(event){
event.preventDefault();
var form_status = $('<div class="form_status"></div>');
$.ajax({
url: $(this).attr('action'),
method: 'post',
data: $(this).serialize(),
beforeSend: function(){
form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Email is sending...</p>').fadeIn() );
}
}).done(function(data){
form_status.html('<p class="text-success">Thank you for contact us. As early as possible we will contact you</p>').delay(3000).fadeOut();
});
});