我再次。
我有一些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;
}
答案 0 :(得分:1)
您的代码会覆盖下一个错误。为避免这种情况,请连接消息:
$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包含标识为ul
的{{1}}或ol
div,并且正确加载了jQuery。
答案 1 :(得分:-1)
您可能希望使用+=
,而不是=
,后者会覆盖以前的值。您还希望将echo
语句放在if
的字符串中。你原来的代码在顶部有很多瑕疵;剩下的东西来自回应各种<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请求应该有效。