質問

I want to initialize a flag in a condition in PHP and send it to be read by 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't seem to retrieve the Flag in JavaScript.

役に立ちましたか?

解決

I found out why my Flag displays Undefined. It is simply that to make a alert (flagQuery) a boolean does not function.

I combine my code with that of Jan Turon, 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

他のヒント

Just uncomment the echo $flagQuery from your PHP code and change it to

echo $flagQuery ? 1 : 0;

since false echoes nothing.

A stands for asynchronous in AJAX. Therefore in your javascript you need to set the flag var in onreadystatechange event:

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);
}

Your code will also work if you make it synchronous by setting false as the third parameter in:

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

and if you actualy return the responseText from the getValue() function, but I don't recommend it since the net communication may fail and you javascript then freezes.

BTW: you don't need to bother with ActiveXObject legacy code, see here. Also have a look at XHR2: I updated your JS code for XHR2 standard in my answer), as you see, it is much shorter.

Oh, and there is a minor mistake in your PHP, you need to echo the flagQuery everytime:

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

echo $flagQuery ? 1 : 0; // outside of the if-else block
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top