Home >Backend Development >PHP Tutorial >Is there a way in jquery to determine whether the result of a form submission is successful or unsuccessful?
The specific thing is this. When I was working on a project today, I used the layer pop-up plug-in. What I popped up was an iframe layer. I used ajax submission, and used the callback after ajax submission to determine whether the submission was successful. If successful, call parent.layer.close(index); close the pop-up box and refresh the current page outside to get the latest data.
But here comes the problem. Now I need to add a form verification. I plan to use jquery validate for verification, but when I submit it, I use ajax without going through the form tag. So I would like to ask if there is any way to determine whether the result of a form submission is successful or unsuccessful
Form:
<code class="html"> <div class="ibox-content"> <div class="col-md-12"> <div class="form-group"> <label class="col-sm-3 control-label">用户组名称:</label> <div class="col-sm-9"> <input id='name' name="name" class="form-control" placeholder="用户组名称" type="text" value="{$info.name}"> <span class="help-block m-b-none id='user_help' ">输入用户组名称,不能少于3个字</span> </div> </div> <div class="form-group"> <label class="col-sm-3 control-label">所属上级组名称 :</label> <div class="col-sm-9"> <select class="form-control" name="pid[]" id='pid'> {$a} </select> <span class="help-block m-b-none">请下拉选择该分组的所属上级,若该分组没有所属上级,则选择最上级</span> </div> </div> <div class="form-group"> <label class="col-sm-6 control-label">组详细说明:</label> <div class="col-sm-6"> <textarea id='description' name="description" placeholder="输入组详细说明" style="height: 120px; width: 640px">{$info.description}</textarea> </div> </div> <input type="hidden" id='id' name="id" value="{$info.id}"> <button class="btn btn-primary" id='button'>提交</button> </div> </div> js ajax提交</code>
<code> var url = "{:U('info')}"; var index = parent.layer.getFrameIndex(window.name); $('#button').click(function(){ $.post(url,{ 'name' : $('#name').val(), 'pid' : $('#pid').val(), 'description' : $('#description').val(), 'id' : $('#id').val() },function(data){ if(data.status != 1){ alert(data.info); }else{ alert(data.info); parent.layer.close(index); //执行关闭 } }) }) </script></code>
The specific thing is this. When I was working on a project today, I used the layer pop-up plug-in. What I popped up was an iframe layer. I used ajax submission, and used the callback after ajax submission to determine whether the submission was successful. If successful, call parent.layer.close(index); close the pop-up box and refresh the current page outside to get the latest data.
But here comes the problem. Now I need to add a form verification. I plan to use jquery validate for verification, but when I submit it, I use ajax without going through the form tag. So I would like to ask if there is any way to determine whether the result of a form submission is successful or unsuccessful
Form:
<code class="html"> <div class="ibox-content"> <div class="col-md-12"> <div class="form-group"> <label class="col-sm-3 control-label">用户组名称:</label> <div class="col-sm-9"> <input id='name' name="name" class="form-control" placeholder="用户组名称" type="text" value="{$info.name}"> <span class="help-block m-b-none id='user_help' ">输入用户组名称,不能少于3个字</span> </div> </div> <div class="form-group"> <label class="col-sm-3 control-label">所属上级组名称 :</label> <div class="col-sm-9"> <select class="form-control" name="pid[]" id='pid'> {$a} </select> <span class="help-block m-b-none">请下拉选择该分组的所属上级,若该分组没有所属上级,则选择最上级</span> </div> </div> <div class="form-group"> <label class="col-sm-6 control-label">组详细说明:</label> <div class="col-sm-6"> <textarea id='description' name="description" placeholder="输入组详细说明" style="height: 120px; width: 640px">{$info.description}</textarea> </div> </div> <input type="hidden" id='id' name="id" value="{$info.id}"> <button class="btn btn-primary" id='button'>提交</button> </div> </div> js ajax提交</code>
<code> var url = "{:U('info')}"; var index = parent.layer.getFrameIndex(window.name); $('#button').click(function(){ $.post(url,{ 'name' : $('#name').val(), 'pid' : $('#pid').val(), 'description' : $('#description').val(), 'id' : $('#id').val() },function(data){ if(data.status != 1){ alert(data.info); }else{ alert(data.info); parent.layer.close(index); //执行关闭 } }) }) </script></code>
Ajax can also submit forms
After the form is submitted, based on the operation results of the database, it is judged which page to jump to, success or failure
After requesting data based on ajax, the returned results can be judged
sucess can determine whether the ajax data is successfully transmitted to the server, and the data returned by the server determines whether the server has successfully processed it