dsfliu1129 2017-08-02 11:07
浏览 28
已采纳

如何从php检索布尔值到javascript文件

is that possible to retrieve Boolean value from php to javascript?

All I want to do is simple: Retrieve Boolean value from php variable $x into js

  1. Should be true when emails are sent

  2. And False when emails are not sent

Then take that Boolean value with javascript print the appropriate message

Complete code of my work can be found here, on my other case I had opened yesterday

PHP:

if (!$mail->send()) {
   $x = false; //when email is not sent
} else {
    $x = true; //when email is sent
}

JS Pseudocode

.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");
    }
});
  • 写回答

3条回答 默认 最新

  • donglian4770 2017-08-02 11:12
    关注

    I am assuming you are already passing data (via AJAX) you just don't know how to encode it.

    Use: 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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?