0

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");
    }
});
7
  • 2
    What you're looking for is called AJAX Commented Aug 2, 2017 at 11:09
  • or define your javascript variable in a php executable file then call that variable from your js file Commented Aug 2, 2017 at 11:09
  • It would be helpful to post an answer of your thoughts, don't worry I won't down vote if they won't work :) Commented Aug 2, 2017 at 11:11
  • @Eternal Are you posting your from through ajax ? Commented Aug 2, 2017 at 11:14
  • @SaadSuri yes look the complete code in the link I posted in my question Commented Aug 2, 2017 at 11:15

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

11 Comments

yes I am using ajax, look the complete code in the link I posted in my question
make sure set dataType: 'json' also and/or send proper content type header
your else is missing the open {
Nice but it works only when the emails are sent. Nothing happens when emails aren't sent
Is it because I switched the true and false from your code? $x = true should have been $x = false in my code.
|
0

While returning bool value from PHP code use json_encode(). It convert from native PHP types to native Javascript type.

http://php.net/manual/en/function.json-encode.php

Comments

0

using response code and handling them with done and fail

PHP

<?php
  // ...

  $success = '{"message": "your mail is send"}';
  $error = '{"message": "failed sending mail"}';

  $code;
  $out;
  if(mailSend){
    $code = 200;
    $out = $success;
  } else {
    $code = 400;
    $out = $error;
  }

  http_response_code($code);
  echo $out;

JS

// ...
var data = 'yourData';

$.ajax({
  url: 'yourApiUrl'M,
  data: data,
  method: 'POST',
  xhrFields: {withCredentials: true},
  crossDomain: true,
  dataType: 'json'
}).done(function() {
  // mail was send
}).fail(function() {
  // error on sending
});

a list of status codes you can find on https://en.wikipedia.org/wiki/List_of_HTTP_status_codes

4 Comments

url to api you mean php file path ?
something is not working properly and the page reloads
@Eternal, url is the domain you can attache your php script through. bacause php is server based the relative path to the file only works if it's in the same base base folder as the the js file
no it doesn't matter, I am using ../phpfile.php and works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.