[英]Ajax form submit no result
我有一個注冊頁面。 如果所有字段都正確,我會進行 JQuery 驗證,然后對 regForm.php 進行 AJAX 調用以上傳新用戶 - 非常基本。
我的問題
驗證正在工作,但是,當用戶注冊時,什么也沒有發生。 我沒有收到成功或錯誤消息,沒有寫入數據庫,Chrome 上的控制台也沒有顯示任何內容。 所以我被困住了。
我確定這只是一個小問題......也許我錯過了什么?
HTML
<div id="ajaxMsgDiv"></div>
<form name="registration-form" id="regForm" method="post" action="regForm.php">
<span id="sname-info" style="color:red"></span>
Name: <br /><input type="text" id="sname" name="fName" onfocus="this.value='';" /><br />
<span id="ssurname-info" style="color:red"></span>
Surname:<br /> <input type="text" id="ssurname" name="fSurname" onfocus="this.value='';" /> <br />
<span id="sspword-info" style="color:red"></span>
Password:<br /><input type="text" id="spword" name="fPword" onfocus="this.value='';" /><br />
<span id="sconfirmpword-info" style="color:red"></span>
Confirm Pword:<br /><input type="text" id="sconfirmpword" name="pwordConfirm" onfocus="this.value='';" /><br />
<span id="sEmail" style="color:red"></span>
Email:<br /><input type="text" id="sEmail" name="sEmail" onfocus="this.value='';" /><br />
<span id="sPhone-info" style="color:red"></span>
Phone:<br /><input type="text" name="sPhone" id="sPhone" onfocus="this.value='';" /><br />
Male<input type="radio" value="male" name="sex">Female<input type="radio" value="female" name="sex"><br />
<input type="submit" onclick="" value="Register" name="register" class="buttono" />
</form>
查詢
<script type="text/javascript">
$(function () {
var form = $('#regForm');
var formMessages = $('#ajaxMsgDiv');
// Set up an event listener for the contact form.
$(form).submit(function (e) {
// Stop the browser from submitting the form.
e.preventDefault();
//do the validation here
if (!validateReg()) {
return;
}
// Serialize the form data.
var formData = $(form).serialize();
// Submit the form using AJAX.
$.ajax({
type: 'POST',
url: $(form).attr('action'),
data: formData
}).done(function (response) {
// Make sure that the formMessages div has the 'success' class.
$(formMessages).removeClass('error').addClass('success');
// Set the message text.
$(formMessages).html(response); // < html();
// Clear the form.
$('').val('')
}).fail(function (data) {
// Make sure that the formMessages div has the 'error' class.
$(formMessages).removeClass('success').addClass('error');
// Set the message text.
var messageHtml = data.responseText !== '' ? data.responseText : 'Oops! An error occured and your message could not be sent.';
$(formMessages).html(messageHtml); // < html()
});
});
function validateReg() {
var valid = true;
if (!$("#sname").val()) {
$("#sname-info").html("(required)");
$("#sname").css('background-color', '#FFFFDF');
valid = false;
}
if (!$("#ssurname").val()) {
$("#ssurname-info").html("(required)");
$("#ssurname").css('background-color', '#FFFFDF');
valid = false;
}
if (!$("#spword").val()) {
$("#spword-info").html("(required)");
$("#spword").css('background-color', '#FFFFDF');
valid = false;
}
if(!$("#sEmail").val()) {
$("#sEmail-info").html("(required)");
$("#sEmail").css('background-color','#FFFFDF');
valid = false;
}
if(!$("#sEmail").val().match(/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/)) {
$("#sEmail-info").html("(invalid Email)");
$("#sEmail").css('background-color','#FFFFDF');
valid = false;
}
if(!$("#sPhone").val()) {
$("#sPhone-info").html("(required)");
$("#sPhone").css('background-color','#FFFFDF');
valid = false;
}
return valid;
}
})
您的驗證函數總是返回“false”,這就是不發送請求的原因。 發生這種情況是因為電子郵件驗證從未通過:
if(!$("#sEmail").val()) {
}
反過來,發生這種情況是因為您有一個跨度和一個具有相同 ID 的輸入。 嘗試改變它:
<span id="sEmail" style="color:red"></span>
看我的小提琴: http : //jsfiddle.net/9cs65xfn/
在嘗試獲取響應之前,您可能希望確保發送請求。 使用各種開發工具,如firebug
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.