在OL中打印JSON错误
Print JSON Errors in OL
我又来了。
我有一些由创建如下的PHP生成的错误:
if (strlen($event_name) < 1){ echo $result['error'] = "<li>You must enter an event name</li>";}
if (strlen($venue_name) < 1){ echo $result['error'] = "<li>You must enter a venue name</li>";}
if (strlen($event_date) < 1){ echo $result['error'] = "<li>You must enter an event date - if not please write TBC</li>";}
if (strlen($address_l1) < 1){ echo $result['error'] = "<li>You must enter an address</li>";}
if (strlen($postcode) < 1){ echo $result['error'] = "<li>You must enter a postcode</li>";}
if (strlen($country) < 1){ echo $result['error'] = "<li>You must enter a country</li>";}
最后我得到了:
echo json_encode($result);
要将这些错误打印到我的OL,我有这样的:
$('#errorbox').html(result.error);
然而,没有任何结果……就像它没有出现一样。
编辑:这是AJAX完成后调用的函数:
}, function(result) {
if (result.success == true) {
$('#errorbox').html(result);
} else if (result.success == false) {
$('#errorbox').html("Not working dude!");
}
}, 'json');
return false;
}
您的代码将用下一个错误覆盖任何错误。为了避免这种情况,请连接消息:
$result['error'] = '';
if (strlen($event_name) < 1){ $result['error'] .= "<li>You must enter an event name</li>";}
if (strlen($venue_name) < 1){ $result['error'] .= "<li>You must enter a venue name</li>";}
if (strlen($event_date) < 1){ $result['error'] .= "<li>You must enter an event date - if not please write TBC</li>";}
if (strlen($address_l1) < 1){ $result['error'] .= "<li>You must enter an address</li>";}
if (strlen($postcode) < 1){ $result['error'] .= "<li>You must enter a postcode</li>";}
if (strlen($country) < 1){ $result['error'] .= "<li>You must enter a country</li>";}
还要确保HTML包含id为errorbox
的ul
或ol
div,并且jQuery已正确加载。
您可能想要使用+=
,而不是=
,后者会覆盖以前的值。你还想把echo
语句放在你的if
s字符串中。你的原始代码在顶部有很多漏洞;来自各种<li>
s的回声的残余物。
$result['error']="";
if (strlen($event_name) < 1) { $result['error'] .= "<li>You must enter an event name</li>";}
if (strlen($venue_name) < 1) { $result['error'] .= "<li>You must enter a venue name</li>";}
if (strlen($event_date) < 1) { $result['error'] .= "<li>You must enter an event date - if not please write TBC</li>";}
if (strlen($address_l1) < 1) { $result['error'] .= "<li>You must enter an address</li>";}
if (strlen($postcode) < 1) { $result['error'] .= "<li>You must enter a postcode</li>";}
if (strlen($country) < 1) { $result['error'] .= "<li>You must enter a country</li>";}
现在,对PHP页面的GET请求应该可以工作了。
相关文章:
- ajax和json错误.php帖子错误
- json错误消息,可以't请参阅SyntaxError
- json错误json.parse:意外的数据结束错误
- 如何处理JSON错误消息
- D3.js 制作树时的 json 错误
- 评估 json 错误
- 使用 Web API MS CRM 时的 JSON 错误
- Javascript/JSON 错误:格式不正确
- JSON 错误:无法读取未定义的属性“xxx”
- 如何序列化全日历源.将循环结构转换为 JSON 错误
- jquery json错误,当我想从Steam市场获取JSON时
- Facebook javascript JSON 错误消息
- 控制台中的JSON错误:意外的令牌:
- 如何摆脱Firefox中的JSON错误
- 使用jQuery$.each,Json错误,结果为1
- 文件不存在错误,package.json错误
- JSON错误:应为..得到'未定义'
- 节点js中的JSON错误未定义为发布数据的前缀
- php后端出现JSON错误
- Jquery Json错误,来自steam的Json,访问控制允许来源