我使用Bootstrap 4和PHPMailer创建了一个表单提交,它完美地运行,直到我添加一些jQuery来尝试显示一个警告框来显示成功或失败。
`
<div id="messages" class="hide" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<div id="messages_content"></div>
</div>
<form id="form" role="form" action="test.php" method="post" enctype="multipart/form-data" class="form">
<div class="row">
<div class="col-md-3">
<label class="input-group g-rounded-3 g-mb-0--md">
<input class="form-control g-brd-white g-brd-right-none border border-secondary border-right-0 u-has-success-v1-2" type="text" placeholder="Your name" name="name" required>
<div class="input-group-append">
<span class="input-group-text g-bg-white g-color-gray border border-secondary border-left-0"><i class="fa fa-user"></i></span>
</div>
</label>
</div>
<div class="col-md-3">
<label class="input-group g-rounded-3 g-mb-0--md">
<input class="form-control g-brd-white g-brd-right-none border border-secondary border-right-0" type="tel" placeholder="Your phone number" name="number" required>
<div class="input-group-append">
<span class="input-group-text g-bg-white g-color-gray border border-secondary border-left-0"><i class="fa fa-phone"></i></span>
</div>
</label>
</div>
<div class="col-md-4">
<select class="custom-select w-100 mr-sm-3 mb-3 mb-lg-0 border border-secondary" id="inlineFormCustomSelect" name="service">
<option selected="">Choose Services...</option>
<option value="General Enquiry">General Enquiry</option>
<option value="Light Demolition">Light Demolition</option>
<option value="Rubbish Clearance">Rubbish Clearance</option>
<option value="House Clearance">House Clearance</option>
</select>
</div>
<div class="col-md-2">
<button class="btn btn-md u-btn-white text-uppercase g-font-size-12 g-font-weight-600 g-rounded-5 border border-secondary bg-white btn-block" type="submit">Request Call Back</button>
</div>
</div>
</form>
</section>
</div>
</div>
</section>
<script src="http://code.jquery.com/jquery.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script>
$('#form').submit(function(e) {
$('#messages').removeClass('hide').addClass('alert alert-success alert-dismissible').slideDown().show();
$('#messages_content').html('<h4>MESSAGE HERE</h4>');
$('#modal').modal('show');
e.preventDefault();
});
</script>`
提交表单后,将显示警告框,其中文本字段仍然填充(显然未在任何地方发送),并且未收到任何电子邮件。
如果我删除jQuery,表单将被发送到php文件,处理并返回页面并收到电子邮件。我似乎无法找到这个具体问题,我无法看到问题!
问题出在jQuery的底部:
e.preventDefault();
这可以防止表单提交事件的默认操作,因此表单将不会提交。删除此行,它将工作。