如果我保留電子郵件和/或密碼字段爲空並嘗試提交表單,它會向我提供客戶端錯誤消息以填充輸入框中的所需數據。如何使用AJAX顯示不同的成功/錯誤PHP消息
一旦我在表單頁面上輸入正確的電子郵件和密碼後提交表格,它會在數據庫表格中插入數據並給我成功的信息。
但是,如果我在表單頁面上輸入不正確的電子郵件和/或祕密代碼後提交表單,由於服務器端驗證腳本我已經寫入,它不會在數據庫表中插入數據,但仍然給我成功消息而不是給出特定不正確的電子郵件和/或密碼的錯誤消息。
我需要做些什麼來顯示特定的錯誤消息,我從服務器端頁面獲取不正確的數據?
客戶端頁面(contact.js)
$(function() {
$("#MyFormName input).jqBootstrapValidation({
preventSubmit: true,
submitError: function($form, event, errors) {
// something to have when submit produces an error ?
// Not decided if I need it yet
},
submitSuccess: function($form, event) {
event.preventDefault(); // prevent default submit behaviour
// get values from FORM
var emailid = $("input#emailid").val();
var secretcode = $("input#secretcode").val();
$.ajax({
url: "./contact_p.php",
type: "POST",
data: {
emailid: emailid,
secretcode: secretcode
},
cache: false,
success: function() {
// Success message
$('#success').html("<div class='alert alert-success'>");
$('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-success')
.append("<strong>Success Message will come here.</strong>");
$('#success > .alert-success')
.append('</div>');
//clear all fields
$('#FormPRegister').trigger("reset");
},
error: function() {
// Fail message
$('#success').html("<div class='alert alert-danger'>");
$('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-danger').append("<strong>Sorry " + " Error Message will come here!");
$('#success > .alert-danger').append('</div>');
//clear all fields
$('#FormPRegister').trigger("reset");
},
})
},
filter: function() {
return $(this).is(":visible");
},
});
$("a[data-toggle=\"tab\"]").click(function(e) {
e.preventDefault();
$(this).tab("show");
});
});
/*When clicking on Full hide fail/success boxes */
$('#r_name').focus(function() {
$('#success').html('');
});
服務器端頁面(contact_p.php)
if(empty($str_emailid))
{
echo "No Email ID entered!";
return false;
}
if(empty($str_secretcode) || $_SESSION['image_secret_code'] != $str_secretcode)
{
echo "Invalid Secret Code!";
return false;
}
$str_insert = "INSERT INTO t_contact (emailid,idate,ipaddress) VALUES('".$str_emailid."','".date("Y-m-d H:i:s")."','".$_SERVER['REMOTE_ADDR']."')";
RunQuery($str_insert);
return true;
Ty爲您的答覆。是否有任何教程或指導或幫助我可以學習和實施你在答案中提出的建議。這對我非常有幫助。 –
不確定,但我想你可以開始使用基本的jQuery和PHP教程,谷歌應該幫助你找到一些。 這需要練習我猜。 –