我收到了一个联系表,会发送电子邮件并写入数据库。问题是它会重定向到新页面。
index.php jQuery代码:
$( "#sendform" ).submit(function(e) {
//e.preventDefault();
//alert( "Handler for .submit() called." );
});
detail.php表格:
<form name="contactform" id="sendform" method="POST" action="<?php echo $editFormAction;?>">
<div id="links">
<h2>Uw Boodschap</h2>
<textarea name="message"></textarea>
</div>
<div id="rechts">
<img src="" id="postzegel" name="postzegel" width="80" height="64" class="kaart"/>
<input type="hidden" name="kaartURL" id="kaartURL" value="" />
<input type="hidden" name="kaartNr" id="kaartNr" value="<?php echo $kaartNr?>"/>
<h2 class="afzender">Afzender</h2>
<input type="text" value="Uw naam" name="naamzender" onfocus="if (this.value == 'Uw naam') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Uw naam';}" maxlength="50" size="33">
<input type="text" value="Uw Email" name="emailzender" onfocus="if (this.value == 'Uw Email') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Uw Email';}" maxlength="50" size="33">
<h2>Ontvanger</h2>
<input type="text" value="Naam" name="naamontvanger" onfocus="if (this.value == 'Naam') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Naam';}" maxlength="50" size="33">
<input type="text" value="Email"name="emailontvanger" onfocus="if (this.value == 'Email') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Email';}"maxlength="50" size="33">
<input type="submit" id="submit" value="Verzend mijn Ecard" >
</div>
<input type="hidden" name="MM_insert" value="contactform" />
</form>
我已设置此变量$editFormAction = $_SERVER['PHP_SELF'];
我将重定向设置为
$insertGoTo = "index.php"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo));
如果我省略重定向,它将在提交后重定向到detail.php,如果我使用e.preventDefault
它将不会执行电子邮件/数据库php代码。我想执行电子邮件/数据库php代码而无需更改或重新加载页面。
答案 0 :(得分:0)
试试这个会起作用
$("#sendform").on('submit', function (e) {
e.preventDefault();
console.log($(this).attr('action'));
console.log($(this).serialize());
$.ajax({
url: $(this).attr('action'),
data: $(this).serialize()
}).done(function (data) {
if (console && console.log) {
console.log("form submitted Nice Aaah");
}
});
});