显示多个错误导致的Ajax的jQuery [英] Show multiple Error result in Ajax jQuery
本文介绍了显示多个错误导致的Ajax的jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I'm试图展示在同一时间多个错误的结果(如果有超过1错误结果)显示中一个接一个的来代替。为了让用户看到了总误差一下子让他们能够在同一时间修复。
下面是我的code: 在主页
<形式ID =iformNAME =iform的onsubmit =的validate();返回false>
<表格的宽度=509>< TR>
< TD WIDTH =114>名用户和LT; / TD>
< TD WIDTH =177><输入类型=文本名称=所有者ID =所有者/>< / TD>< TD ID =ERR1WIDTH =202的风格=颜色:红色;>< / TD>< / TR>< TR>
< TD>电子邮件< / TD>< TD><输入类型=电子邮件NAME =电子邮件ID =电子邮件/>< / TD>< TD ID =ERR2的风格= 颜色:红色;>< / TD>< / TR>< TR>
< TD>贝宝< / TD>
< TD><输入类型=电子邮件NAME =贝宝ID =贝宝/>< / TD>< TD ID =ERR3的风格=颜色:红色;>< / TD>&所述; / TR>&其中; TR>
< TD>&安培; NBSP;< / TD>
< TD><输入类型=提交值=提交/>< / TD>< / TR>
< /表>
< /形式GT;
阿贾克斯
$。阿贾克斯({
网址:engine.php,
数据:{
动作:ADD_USER,
老板:document.iform.owner.value,
电子邮件:document.iform.email.value,
贝宝:document.iform.paypal.value
},
键入:POST,
成功:功能(数据){
如果(数据==ER1){
$(#ERR2)HTML(该电子邮件地址已经存在,请尝试其他)。
返回false;
}
如果(数据==ER2){
$(#ERR3)HTML(这贝宝帐户已经存在,请尝试其他)。
返回false;
}
如果(数据==确定){
location.reload();
}
}
});
在PHP
$所有者= $ _ POST [车主]
$电子邮件= $ _ POST [邮件]
$贝宝= $ _ POST [贝宝]
$ SS = $ conn-> prepare('选择email_owner,从tb_owner paypal_owner');
$ SS->执行();
$渡= $ SS->取();
如果($电子邮件== $渡['email_owner']){
回声ER1;
}
否则,如果($贝宝== $渡['paypal_owner']){
回声ER2;
}
其他{
/ * EVEYTHING正常 - >接入到数据库* /
如果($ stmt-> rowCount等()){
回声确定;
}
}
解决方案
解决这个很简单:[JSON] [1]
在你的PHP你应该是这样的:
$错误=阵列();
如果($电子邮件== $渡['email_owner']){
$错误[] =这封电子邮件是已经存在,尝试另一种;
}
如果($贝宝== $渡['paypal_owner']){
$错误[] =这是贝宝已经存在,尝试另一种;
}
如果(计数($错误)大于0){
回声json_en code(阵列('地位'=>'错误','错误'=> $错误));
然后你就可以在Javascript阅读这样的:
成功:功能(数据){
变种JSON = $ .parseJSON(数据);
如果(json.status ==='错误'){
$(#ERR2)的HTML(json.errors [0]);
$(#ERR3)的HTML(json.errors [1]);
返回false;
}
如果(json.status ===成功){
location.reload();
}
I´m trying to show multiple error result at the same time(if there are more than 1 error result) in stead of showing one by one. To let user see the total error at once so they can fix at one time.
Here is my code: In main page
<form id="iform" name="iform" onsubmit="validate(); return false">
<table width="509"><tr>
<td width="114">Name User</td>
<td width="177"><input type="text" name="owner" id="owner"/></td><td id="err1" width="202" style="color:red;"></td></tr><tr>
<td>Email </td><td><input type="email" name="email" id="email"/></td><td id="err2" style="color:red;"></td></tr><tr>
<td>Paypal</td>
<td><input type="email" name="paypal" id="paypal" /></td><td id="err3" style="color:red;"></td></tr><tr>
<td> </td>
<td><input type="submit" value="Submit" /></td></tr>
</table>
</form>
Ajax
$.ajax({
url:'engine.php',
data:{
action:'add_user',
owner:document.iform.owner.value,
email:document.iform.email.value,
paypal:document.iform.paypal.value
},
type:'POST',
success: function(data){
if (data=="er1"){
$("#err2").html("This email is already existed, please try another");
return false;
}
if (data=="er2"){
$("#err3").html("This paypal account is already existed, please try another");
return false;
}
if (data=="ok"){
location.reload();
}
}
});
in PHP
$owner=$_POST[owner];
$email=$_POST[email];
$paypal=$_POST[paypal];
$ss = $conn->prepare('select email_owner, paypal_owner from tb_owner');
$ss->execute();
$watashi=$ss->fetch();
if ($email==$watashi['email_owner']){
echo"er1";
}
else if ($paypal==$watashi['paypal_owner']){
echo"er2";
}
else{
/* EVEYTHING IS OK ->INSERT TO DATABASE */
if ($stmt->rowCount()){
echo"ok";
}
}
解决方案
The solution to this is simple: [JSON][1].
In your PHP you should have something like:
$errors = array();
if($email==$watashi['email_owner']){
$errors[] = "this email is already existed, try another";
}
if($paypal==$watashi['paypal_owner']){
$errors[] = "this paypal is already existed, try another";
}
if(count($errors) > 0){
echo json_encode(array('status' => 'error', 'errors' => $errors));
And then you can read this in Javascript like this:
success: function(data){
var json = $.parseJSON(data);
if(json.status=== 'error'){
$("#err2").html(json.errors[0]);
$("#err3").html(json.errors[1]);
return false;
}
if(json.status==="success"){
location.reload();
}
这篇关于显示多个错误导致的Ajax的jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文