简体   繁体   English

使用JQuery,Ajax,PHP,Json进行表单验证

[英]Form validation using JQuery, Ajax, PHP, Json

I am trying to verify values that users are typing in the field. 我正在尝试验证用户在字段中键入的值。

And I ran across a problem regarding email field and email confirmation field. 我遇到了有关电子邮件字段和电子邮件确认字段的问题。

How I proceed; 我如何进行;

The user type in his email address, an OnFocus and OnChange event are triggered. 用户键入其电子邮件地址, OnFocusOnChange事件将被触发。 But the browser tend to auto-fill the email confirmation field therefor triggering the OnChange event. 但浏览器倾向于自动填充电子邮件确认字段,从而触发OnChange事件。

So what happen is that the email typed in the first field get sent to the server to validate it and check if its already in the database. 那么所发生的是,在第一个字段中输入的电子邮件被发送到服务器以验证它并检查它是否已经在数据库中。 If this email pass all the test I store it in a temporary $_SESSION[$tmp_var] . 如果此电子邮件通过了所有测试,我将其存储在临时$_SESSION[$tmp_var] However, the email confirmation field get triggered in the same time due to auto-fill and sometime get to the server to be compared with $_SESSION[$tmp_var] which is not always set because the process is a bit longer. 但是,由于自动填充,电子邮件确认字段会在同一时间触发,有时会到服务器与$_SESSION[$tmp_var]进行比较,因为进程有点长,所以$_SESSION[$tmp_var]并不总是设置。

I know that the attribute autofill="off" is not an option. 我知道属性autofill="off"不是一个选项。 I use $_SESSION[$tmp_var] because non-Javascript users get to reload the page if there is an error and the field are filled with this so they don't have to retype their values. 我使用$_SESSION[$tmp_var]因为如果出现错误并且字段被填充,非Javascript用户可以重新加载页面,这样他们就不必重新键入它们的值。

I would like to keep the auto-fill of the form because it save user's time. 我想保留表单的自动填充,因为它节省了用户的时间。

Here is my JQuery that send json arrays to PHP; 这是我的JQuery将json数组发送到PHP;

$("body").on("focusout change", "input, select", function () {
    var id = ($(this).attr("id"));
    var container = ['#' + id + "_ck", '#' + id + '_err'];
    var data_type = ($(this).attr("data-type"));
    var text = document.getElementById(id).value;
    $.ajax({
        url: 'ajax-php.php',
        type: 'post',
        dataType: 'json',
        data: { 'action': 'input_ck', 'id': id, 'text': text, 'data_type': data_type },
        success: function (data, status) {
                if (data[2] == 'true') {
                     $(container[1]).addClass("err_f");
                     $(container[1]).html(data[1]);
                     $(container[0]).html(data[0]);
                      }
                if (data[2] == 'false') { 
                     $(container[1]).addClass("pass_f"); 
                     $(container[1]).html('Valeur acceptée.');
                     $(container[0]).html(data[0]);
                }
        },
        error: function (xhr, desc, err) {
            console.log(xhr);
            console.log("Details: " + desc + "\nError:" + err);
        }
    }); 
});      

The first thing that comes to my mind would be to intercept the field id of email confirmation and compare it with the email field value 我想到的第一件事就是拦截电子邮件确认的字段ID,并将其与电子邮件字段值进行比较

Or somehow wait for one query to end with success before performing another query so both email and email confirmation field would not be sent at the same time; 或者以某种方式等待一个查询在执行另一个查询之前以成功结束,这样电子邮件和电子邮件确认字段就不会同时发送;

Any suggestions? 有什么建议么?

If you want to stick with your existing code, some little changes will fix your issue. 如果您想坚持使用现有代码,一些小的更改将解决您的问题。 Add an additional input field with empty field value and fill it with some session variable output in the success function of ajax request. 添加一个带有空字段值的附加输入字段,并在ajax请求的success函数中填充一些会话变量输出。

check for the value of the input field in the success function, until you get a value, loop it using setTimeout, in this way you can make the ajax request wait till the processing on the php script is done. 检查success函数中输入字段的值,直到得到一个值,然后使用setTimeout循环它,这样你就可以让ajax请求等到php脚本上的处理完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM