我需要幫助我發現這個tutorial驗證窗體沒有刷新,但問題是我認爲這只是文本字段:)但我使用4個不同的單選按鈕我真的需要這個腳本來處理這些單選按鈕!驗證窗體沒有刷新「單選按鈕不起作用」
這裏是JavaScript文件:)!
runOnLoad(function(){
$("input#name").select().focus();
});
$(function() {
$('.error').hide();
$(".button").click(function() {
// validate and process form here
$('.error').hide();
var name = $("input#name").val();
if (name == "") {
$("label#name_error").show();
$("input#name").focus();
return false;
}
var email = $("input#email").val();
if (email == "") {
$("label#email_error").show();
$("input#email").focus();
return false;
}
var mobile = $("input#mobile").val();
if (mobile == "") {
$("label#mobile_error").show();
$("input#mobile").focus();
return false;
}
var college = $("input#college").val();
if (college == "") {
$("label#college_error").show();
$("input#college").focus();
return false;
}
var university = $("input#university").val();
if (university == "") {
$("label#university_error").show();
$("input#university").focus();
return false;
}
var level = $("input#level").val();
if (level == "") {
$("label#level_error").show();
$("input#level").focus();
return false;
}
var first_preference = $("input#first_preference").val();
if (first_preference == "") {
$("label#first_preference_error").show();
$("input#first_preference").focus();
return false;
}
var second_preference = $("input#second_preference").val();
if (second_preference == "") {
$("label#second_preference_error").show();
$("input#second_preference").focus();
return false;
}
var third_preference = $("input#third_preference").val();
if (third_preference == "") {
$("label#third_preference_error").show();
$("input#third_preference").focus();
return false;
}
var heard = $("input#heard").val();
if (heard == "") {
$("label#heard_error").show();
$("input#heard").focus();
return false;
}
var applying = $("input#applying").val();
if (applying == "") {
$("label#applying_error").show();
$("input#applying").focus();
return false;
}
var strength = $("input#strength").val();
if (strength == "") {
$("label#strength_error").show();
$("input#strength").focus();
return false;
}
var weakness = $("input#weakness").val();
if (weakness == "") {
$("label#weakness_error").show();
$("input#weakness").focus();
return false;
}
var previousEx = $("input#previousEx").val();
if (previousEx == "") {
$("label#previousEx_error").show();
$("input#previousEx").focus();
return false;
}
var dataString = 'name='+ name + '&email=' + email + '&mobile=' + mobile + '&college=' + college + '&university=' + university + '&level=' + level + '&first_preference=' + first_preference + '&second_preference=' + second_preference + '&third_preference=' + third_preference + '&heard=' + heard + '&applying=' + applying + '&strength=' + strength + '&weakness=' + weakness + '&previousEx=' + previousEx;
$.ajax({
type: "POST",
url: "php/database_sorting.php",
data: dataString,
success: function() {
$('#contact_form').html("<div id='message'></div>");
$('#message').html("<h2>Contact Form Submitted!</h2>")
.append("<p>We will be in touch soon.</p>")
.hide()
.fadeIn(1500, function() {
$('#message').append("<img id='checkmark' src='images/done.png' />");
});
}
});
return false;
});
});
你能提供你想要應用於單選按鈕的驗證嗎?另外,我應該指出,通過AJAX進行表單提交併不是很好。它指出了人們可以利用的潛在途徑,從中我可以看到我可以直接訪問「php/database_sorting.php」。有人可能會解析一些SQL注入或隨機格式錯誤的數據來嘗試和利用您的腳本。當JavaScript被禁用時,它也不起作用,你應該考慮讓表單驗證服務器端,然後用JS而不是其他方式進行增強,或者純粹跳過服務器端驗證。 – Tom
@Tom SQL注入可以嘗試使用Ajax或不使用,畢竟,您正在向服務器發送請求。我相信OP已經知道,但是,如果你的站點是公開的,你必須進行服務器端驗證。對於未啓用JS的瀏覽器,您可以使用Progressive增強(爲非JS創建回退),或者簡單地在頁面中放置一個'
我想使所有的文本框和單選按鈕和文本區域都是必需的:)!沒有刷新頁面:)!或者至少不會丟失用戶輸入的數據:) – user1634730