0

又是我。

我有一些由 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;
} 
4

2 回答 2

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 包含带有 id的ulor div ,并且 jQuery 已正确加载。olerrorbox

于 2013-04-09T14:11:14.430 回答
-1

您可能想使用+=,而不是=,后者会覆盖以前的值。您还希望将echo语句放在ifs 字符串中。您的原始代码顶部有很多杂乱无章的东西;<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 请求应该可以工作了。

于 2013-04-09T14:09:48.443 回答