Questions & Answers

How to Transfer PHP return variable to Javascript?

stuck why its displaying as undefined instead of returning the correct value. any help would be great. sorry about my awful code. I tried to send the value through cookies and AJAX but still got nothing. Currently i have a call to run the callPHPfunction when the website is loaded then the Test() function runs which runs the Getstatus() that checks the database if the values match a value in the database and return. then the Test() Checks if it has something or not and returns either value as needed.


if (isset($_GET['functionToCall']) && function_exists($_GET['functionToCall'])) {
  function Test() {
    $status = Getstatus();

    if( is_null( $status ) ){
        return "test2";
    if( $status <= 0 ){
        return "test1";


  function Getstatus() {
    $db =& JFactory::getDBO();
    $mc = JRequest::getInt('mc');
    $shift = JRequest::getInt('mc');
    $fields = array();
    $fields[] = "*";

    $where = array();
    $where[] = "`mc` = ".$db->quote( $mc );
    $where[] = "`status` = 0";
    $where[] = "`date` = ".$db->quote(date("Y-m-d"));
    $where[] = "`shift` = ".$db->quote( $shift );

    $query = "SELECT ".implode( ",", $fields );
    $query .= " FROM `maintenance`.`autonomous_job` ";
    $query .= "WHERE ".implode( " AND ", $where );
    $rows = $db->loadObjectList();

    if($db->getErrorNum()) {
      $error = $db->stderr();
    error_log( $error );
    return null;
    return $rows;



function callPHPFunction(mc) {
    var xhttp = new XMLHttpRequest();"POST", "helper.php?functionToCall=Test", true);
        type: "POST",
        url: "alert.php",
        data: {
            task: "Test",
            $mc: mc


function FINISHING(mc){

msg += '<br />'+callPHPFunction();
return false;
2023-01-24 23:01:18
XMLHttpRequest or jQuery.ajax — Pick one. Then read the documentation for how to examine the response. Any Ajax tutorial will explain that. (And your server-side code needs to actually send the data to the client, e.g. with echo just like any other PHP that generates an HTTP response. Ajax isn't a special case.).
2023-01-24 23:01:18
msg += '<br />'+callPHPFunction() - But callPHPFunction() doesn't return anything.
2023-01-24 23:01:18
I see the question has been closed as a duplicate. It isn't a great duplicate because you seem to have 4 or 5 major problems and the duplicate covers the last one that you'll need to handle.
Answers(0) :