duanqiongchong0354 2014-07-14 11:26
浏览 32
已采纳

关于AJAX,FORM和PHP

My ajax code looks like this which check the registration form username, email, etc...

jQuery(document).ready(function($) {
$("#formform").on('change', 'input',function(event){
    var $form = $(this);
    var $inputs = $form.find("input, select, button, textarea");
    var serializedData = $form.serialize();
    $inputs.prop("disabled", true);

    request = $.ajax({
        url: "/registration_check.php",
        type: "post",
        data: {formData:serializedData},
        datetype: "JSON"
    });
    request.done(function (response, textStatus, jqXHR){
        console.log(response);
    });
    request.fail(function (jqXHR, textStatus, errorThrown){
        console.log("error");
    });
    request.always(function () {
        $inputs.prop("disabled", false);
    });
    event.preventDefault();
});
});

And my PHP look like this:

$get_form_data=$_POST["formData"];
parse_str($get_form_data,$form_data);

if(isset($form_data["username"])){
    if(strlen($form_data["username"])<5){
        echo "Username must be at least 5 character";
    }else{
        if(ValidUserName($form_data["username"])){
            if($checkUser->checkUserName(char_encoder($form_data["username"]))==true){
                echo "Sorry this UserName Already Exist";
            }else{
                echo "UserName Available";
            };
        }else{
            echo "Invalid Username";
        }

    }

}

Now How do i disable the form when Invalid Username comes from AJAX as response?

I though i should use return false..but don't know how to handle the response?

  • 写回答

2条回答 默认 最新

  • duanjuelian4640 2014-07-14 11:37
    关注

    try as below format you can handle response in success event:

    var $form = $(this);
    var serializedData = $form.serialize();
    $.ajax({
        type: "POST",
        url: "/registration_check.php",
        data: {
            formData:serializedData
        },
        beforeSend: function () {
            //do stuff like loading process until you get the response
        },
        success: function (resp) {
            var obj = jQuery.parseJSON(resp);
            //console.log(obj); // this will display response in console.
            //do stuff here
    
        },
        error: function(e){
            alert("Error in ajax call: "+e);
        } 
    
    }); // complete AJAX  
    

    PHP CODE:

    $array = array();
    if (isset($form_data["username"])) {
        if (strlen($form_data["username"]) < 5) {
            $array['success'] = false;
            $array['message'] = "Username must be at least 5 character";
        } else {
            if (ValidUserName($form_data["username"])) {
                if ($checkUser->checkUserName(char_encoder($form_data["username"])) == true) {
                    $array['success'] = false;
                    $array['message'] = "Sorry this UserName Already Exist";
                } else {
                    $array['success'] = true;
                    $array['message'] = "UserName Available";
                }
            } else {
                $array['success'] = false;
                $array['message'] = "Invalid Username";
            }
        }
        echo json_encode($array);
    }  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c