Jquery 异步提交表单

2019PHP高薪工程师学习路线图....>>>

前台部分代码:

<div id='error'></div><!--错误提示信息-->

<input type='text' id='username' name='username' readonly='Readonly'/><!--readonly为只读-->

<input type='password' id='password' name='password'>
<input type='text' id='company' name='company'>
<input type='text' id='mobile' name='mobile'>
<input type='text' id='email' name='email'>
<button type='button' onclick='edit();'></button>

<script>
    function edit(){
        var password = $('#password').val();
        var company = $('#company').val();

        if(password == ''){
             $('error').show();
             $('error').html("密码不能为空!");
             return.false;
        }
        else if(company == ''){
             $('error').show();
             $('error').html("公司名称不能为空!");
             return.false;
        }else if(mobile.length<11){
             $('error').show();
             $('error').html("手机号码不正确!");
             return.false;
        }else if(email){
            var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9][email protected]([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$/;
            if(!myreg.test(email)){
                $('#error').show();
                $('#error').html("Email格式不正确!");
                return false;
        }
        else{
            $('#error').hide();
        }
        get_url = "{:U('Index/settingsave')}";
        $.ajax({
            url:get_url,
            type:'POST',
            data:{'password':password,'company':company,'mobile':mobile,'email':email},
            cache:false,
            dataType:"json",
            success:function(data){
                if(data.status == "success"){
                    $('#error').show();
                    $('#error').html(data.Msg);
                }else{
                    $('#error').show();
                    $('#error').html(data.Msg);
                }
            }
        });
        return false;
    }
</script>

后台部分代码:

<?php
    public function settingsave(){

        $user=M("user");
        $id=session("userid");
        $password=I("post.password");
        $data['Company']=I("post.company");
        $data['Mobile']=I("post.mobile");
        $data['Email']=I("post.email");


        if($password!=''){
            $data['Password']=md5($password);
        }
        $condition['Id']=$id;
        $result = $user->where($condition)->save($data);

        if($result !== false) {
            echo  '{"status":"success","Msg":"修改成功"}';

            die();
        }else{
            echo  '{"status":"false","Msg":"修改失败"}';

            die();        

}
    }
?>