Sending a flag from PHP to JavaScript

I want to initialize a flag in state in PHP and send it to read JavaScript.

At the moment I have this code:

Php

if ($totalResults > MAX_RESULT_ALL_PAGES) {

    $queryUrl = AMAZON_SEARCH_URL . 
                $searchMonthUrlParam . 
                $searchYearUrlParam . 
                $searchTypeUrlParam . 
                urlencode( $keyword ) . 
                '&page=' . $pageNum;
} else {

    $queryUrl = AMAZON_TOTAL_BOOKS_COUNT . 
                $searchMonthUrlParam . 
                $searchYearUrlParam . 
                $searchTypeUrlParam . 
                urlencode($keyword) . 
                "&page=" . $pageNum;

    $flagQuery = TRUE;
    echo $flagQuery;
}

Javascript

<script>
    function getFlagValue() {
        var xmlHttp;
        if (window.XMLHttpRequest) {
            xmlHttp = new XMLHttpRequest();
        } else {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState==4 && xmlHttp.status==200) {
                alert(xmlHttp.responseText);
            }
        };
        xmlHttp.open("GET","getAmazonResult.php",true);
        xmlHttp.send();
    }

    var flagQuery = new Boolean();

    flagQuery = getFlagValue();
    alert(flagQuery);
</script>

I can not find the flag in JavaScript.

+4
source share
2 answers

I found out why my flag shows Undefined. Just to make a warning (flagQuery), the boolean does not work.

I combine my code with Jan Turon code and this is achieved

function getFlagValue() {
    var xmlHttp = new HmlHttpRequest();
    xmlHttp.onload = function() {
        if (xmlHttp.status==200) yourCustomHandler(xmlHttp.responseText);
    };
    xmlHttp.open("GET","getAmazonResult.php",true);
    xmlHttp.send();
}

function yourCustomHandler(response) {
    flagQuery = response;
    alert(flagQuery);
}
flagQuery = getFlagValue();
                    if (flagQuery = true) {
                        alert ("Flag = TRUE");
                    }
                    else {
                        alert ("Flag = FALSE");
                    }

And now I see if the flag is true or false

+4
source

Just uncomment echo $flagQueryfrom your PHP code and change it to

echo $flagQuery ? 1 : 0;

because falseit doesn’t output anything.

A AJAX. javascript var onreadystatechange:

function getFlagValue() {
    var xmlHttp = new HmlHttpRequest();
    xmlHttp.onload = function() {
        if (xmlHttp.status==200) yourCustomHandler(xmlHttp.responseText);
    };
    xmlHttp.open("GET","getAmazonResult.php",true);
    xmlHttp.send();
}

function yourCustomHandler(response) {
    flagQuery = response;
    alert(flagQuery);
}

, , false :

xmlHttp.open("GET","getAmazonResult.php",false)

return Text getValue(), , , javascript .

: ActiveXObject , . . XHR2: JS- XHR2 ), , .

, PHP , - flagQuery:

if ($totalResults > MAX_RESULT_ALL_PAGES) {
    $queryUrl = ...
    $flagQuery = FALSE;
} else {
    $queryUrl = ...
    $flagQuery = TRUE;
}

echo $flagQuery ? 1 : 0; // outside of the if-else block
+1

Source: https://habr.com/ru/post/1525385/


All Articles