I have a question in ajax and html forms, And i need your helps please.
我在ajax和html表单中有一个问题,我需要你的帮助。
I'm using PARSLEY validation (http://parsleyjs.org/) for validation rules. But the probleme is that validation doesn't support the database validations : for instant, i have to check if the field 'nom' already exist in database or no, if exist (show a message) and if don't exist submit the form to myaction.php.
我正在使用PARSLEY验证(http://parsleyjs.org/)来验证规则。但问题是验证不支持数据库验证:对于即时,我必须检查字段'nom'是否已存在于数据库中或否,如果存在(显示消息)并且如果不存在则提交表单到myaction.php。
My ajax script :
我的ajax脚本:
<script>
$(document).ready(function () {
$("form").submit(function (e) {
e.preventDefault(); //prevent default form submit
var nom = $("#nom").val();
$.ajax({
type: "POST",
url: 'ajax.php',
data: "field="+ nom ,
success: function (data) {
if(data==1){
alert(data);
}else if(data==0){
cache: false
$('form#form_id').submit();
}
},
});
});
});
</script>
My html form :
我的HTML格式:
<form action="myaction.php" method="post" name="form_name" id="form_id">
<input type="text" id="nom" name="nom" required value="">
<input type="text" id="prenom" name="prenom" required value="">
<input type="text" id="remarque" name="remarque" required value="">
</form>
ajax.php : return 0 if field doesn't exist in database and 1 if exist
ajax.php:如果数据库中不存在字段则返回0,如果存在,则返回1
The probleme i face is that the ajax called in a loop. in each time the form submited and call the ajax. what i want is if data equal 0 (field doesn't exist in database) the form must be submited to myaction.php
我面临的问题是ajax在循环中调用。在每次提交表单并调用ajax时。我想要的是如果数据等于0(数据库中不存在字段),则必须将表单提交给myaction.php
1 个解决方案
#1
Here is one solution.
这是一个解决方案。
Basically you're submitting and handling the submision forever. You need to break that when the data has been validated. Since you only care about nom
, set some flag on it so you can check whenever you're submitting
基本上你是在永久地提交和处理该项目。在验证数据后,您需要打破它。由于您只关心nom,因此请在其上设置一些标记,以便您可以在提交时进行检查
$(document).ready(function () {
$("form").submit(function (e) {
var nom = $('#nom');
var valid = nom.data('valid');
if (valid === 0) { // Is valid, let's continue
return;
} else { // Valid is undefined as was never set
e.preventDefault(); //prevent default form submit
$.ajax({
type: "POST",
url: 'ajax.php',
data: "field="+ nom.val() ,
success: function (data) {
if(data==1) {
alert(data);
} else if(data==0) {
nom.data('valid', 0);
}
}
});
}
});
});
#1
Here is one solution.
这是一个解决方案。
Basically you're submitting and handling the submision forever. You need to break that when the data has been validated. Since you only care about nom
, set some flag on it so you can check whenever you're submitting
基本上你是在永久地提交和处理该项目。在验证数据后,您需要打破它。由于您只关心nom,因此请在其上设置一些标记,以便您可以在提交时进行检查
$(document).ready(function () {
$("form").submit(function (e) {
var nom = $('#nom');
var valid = nom.data('valid');
if (valid === 0) { // Is valid, let's continue
return;
} else { // Valid is undefined as was never set
e.preventDefault(); //prevent default form submit
$.ajax({
type: "POST",
url: 'ajax.php',
data: "field="+ nom.val() ,
success: function (data) {
if(data==1) {
alert(data);
} else if(data==0) {
nom.data('valid', 0);
}
}
});
}
});
});