是否可以从 php 到 javascript 检索 bool 值?
我想做的很简单:从 php 变量 $x
检索 bool 值到 js
Should be true when emails are sent
And False when emails are not sent
然后使用 javascript 获取该 bool 值并打印相应的消息
我的工作完整代码可以在 here 找到,在我昨天打开的另一个案例中
PHP:
if (!$mail->send()) {
$x = false; //when email is not sent
} else {
$x = true; //when email is sent
}
JS 伪代码
.done(function (data) {
if(php Boolean variable is false) {
("$formText").html("Message sent");
} else (if php Boolean variable is true) {
("$formText").html("Message not sent");
}
});
最佳答案
我假设您已经在传递数据(通过 AJAX),但您只是不知道如何对其进行编码。
使用:json_encode()
PHP:
if (!$mail->send()) {
$x = true;
} else {
$x = false;
}
header("Content-type: application/json");
echo json_encode(array('x'=>$x));
Javascript:
.done(function (data) {
if (data.x) {
$("#formText").html("Message sent");
} else {
$("#formText").html("Message not sent");
}
} //end function data
);//end done function
关于javascript - 如何从 php 检索 bool 值到 javascript 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45459174/