gpt4 book ai didi

php - ajax成功查看哪个ECHO自带

转载 作者:行者123 更新时间:2023-12-01 03:36:55 25 4
gpt4 key购买 nike

我的ajax处理脚本中有两种echo。一种用于错误消息,另一种用于表单处理成功。

这就是它的样子。

if (strlen($password) != 128) {
$errorMsg = "<div class='alert alert-danger alert-dismissible' role='alert'>\n";
$errorMsg .= "<strong>Oops!</strong> System error, Invalid password configuration.\n";
$errorMsg .= "</div>\n";
echo $errorMsg;
}

另外一个是

// Print a message based upon the result:
if ($stmt->affected_rows == 1) {
// Print a message and wrap up:
$successMsg = "<div class='alert alert-success alert-dismissible' role='alert'>\n";
$successMsg .= "Your password has been changed. You will receive the new, temporary password at the email address with which you registered. Once you have logged in with this password, you may change it by clicking on the 'Password Modification' link.\n";
$successMsg .= "</div>\n";
echo $successMsg;
}

因此,我使用一个 DIV 在 ajax 成功时填充这些消息。

我的问题是,有没有办法确定ajax成功显示哪条消息?

希望有人能帮助我。谢谢。

最佳答案

您可以使用filter()来查看响应是否具有.alert-danger类:

// $.ajax({ ...
success: function(html) {
var $html = $(html);
if ($html.filter('.alert-danger').length) {
// something went wrong
}
else {
// it worked
}
}

但请注意,更好的模式是返回包含要显示的消息的 JSON,以及警报的类和指示其状态的标志。像这样的事情:

var $arr;

if (strlen($password) != 128) {
$arr = array('success'=>false,'cssClass'=>'alert-danger','message'=>'Oops! System error, Invalid password configuration.');
}

if ($stmt->affected_rows == 1) {
$arr = array('success'=>true,'cssClass'=>'alert-success','message'=>'Your password has been changed. You will receive the new, temporary password at the email address with which you registered. Once you have logged in with this password, you may change it by clicking on the Password Modification link.');
}

echo json_encode($arr);
// $.ajax({ ...
success: function(json) {
if (json.success) {
// it worked
}
else {
// something went wrong
}

// append the alert
$('#myElement').append('<div class="alert alert-dismissible + ' + json.cssClass + '" role="alert">' + json.message + '</div>');
}

关于php - ajax成功查看哪个ECHO自带,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31178856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com