我收到了电子邮件,因为我设置了我的代码,但是当脚本执行时,浏览器会将我重定向到sendemail.php并冻结在那里。
这是截图 sendemail.php
这是我的html文件的FORM元素的代码
</section> -->
<section id="contact">
<div id="contact-us" class="parallax">
<div class="container">
<div class="row">
<div class="heading text-center col-sm-8 col-sm-offset-2 wow fadeInUp" data-wow-duration="1000ms" data-wow-delay="300ms">
<h2>Contact Us</h2>
<p>Request a service , learn about our services</p>
</div>
</div>
<div class="contact-form wow fadeIn" data-wow-duration="1000ms" data-wow-delay="600ms">
<div class="row">
<div class="col-sm-6">
<form id="contact-form" name="contact-form" method="post" enctype="application/x-www-form-urlencoded" action="sendemail.php">
<div class="row wow fadeInUp" data-wow-duration="1000ms" data-wow-delay="300ms" id="message_form">
<div class="col-sm-6">
<div class="form-group">
<input type="text" name="name" id="name" class="form-control" placeholder="Name" required="required">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input type="email" name="email" id="email" class="form-control" placeholder="Email Address" required="required">
</div>
</div>
</div>
<div class="form-group">
<input type="text" name="subject" id="subject" class="form-control" placeholder="Subject" 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" name="submit" id="submit">Send Now</button>
</div>
</form>
</div>
<div class="col-sm-6">
<div class="contact-info wow fadeInUp" data-wow-duration="1000ms" data-wow-delay="300ms">
<p></p>
<ul class="address">
<li><i class="fa fa-phone"></i> <span> Phone:</span> my phone </li>
<li><i class="fa fa-envelope"></i> <span> Email:</span><a href="mailto:info@mydomain.com"> info@mydomain.com</a></li>
<li><i class="fa fa-globe"></i> <span> Website:</span> <a href="#">www.mydomain.com</a></li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
</section><!--/#contact-->
&#13;
这是调用sendemail.php的脚本
<script type="text/javascript">
$(document).ready(function() {
$('#submit').click(function(e) {
var data = name : $("#name").val(),
subject:$("#subject").val ,
email: $("#email").val(),
message: $("#message").val()};
console.log(data);
$.ajax({
type: "post",
url: "sendemail.php",
data: data,
success: function(){
alert("it was success");
}
});
</script>
<!-- begin snippet: js hide: false console: true babel: false -->
&#13;
这是sendemail.php的代码
<?php
$msg = 'Name: ' .$_POST['name'] ."\n"
.'Email :' .$_POST['email'] ."\n"
.'Subject: ' .$_POST['subject'] ."\n"
.'Message: ' .$_POST['message'] ."\n";
mail('info@mydomain.com','You have one message from santoriginal.com', $msg); ?>
我的代码错了?
谢谢
答案 0 :(得分:1)
您的浏览器未冻结,sendemail.php
没有内容。
对sendemail.php
的AJAX调用不会将您重定向到sendemail.php
,但您的表单会这样做。
尝试使用return false;
中的$('#submit').click();
,这样就不会提交表单了。