在单独的javascript函数中使用php网页上的变量值

I have a server running which has a php function which returns true/false depending on input values. Currently I am just echoing the result on the page. I want to use this true/false in a to evaluate a condition in a javascript function running completely separately from the server.

Is there a javascript function I can use to get the text from a webpage and put it in a variable? I looked at the jquery load() function but this doesn't seem like it will work for this purpose.

Keep the output of the PHP script as simple as possible (a text response outputting only "true" or "false").

To send a text response (instead of an HTML response), you can use:

header("Content-Type: text/plain");

You have to call this function before outputting anything.

Now, assuming you can access the output of the script at the URL http://www.example.com/webpage.php

if($.ajax({type: "GET", url: "http://www.example.com/webpage.php", async: false}).responseText == "true")
{
    // do something
}
else // "false"
{
    // do something else
}

Not sure if it would work, but you might try using document.body.innerHTML. It gets you the innerHTML from the body element from the document (which, in your case, should be a true or false string).

If you are just wanting to use jQuery to get the echoed out result of your php you can do:

     var whatever = "<?php echo $result ?>";

In your PHP/HTML (assuming you have a function named "yourFunction" that returns a boolean):

<div id="your_id">
    <?php echo yourFunction()?'true':'false';?>
</div>

And in your JavaScript

if($('#your_id').text() == "true")
    // do something
else
    // do something else

You could treat it like html, update your doc to add a tag around the result. "true" at which point your almost using XML.

You should be able to get the raw response. I usually don't recommend consuming raw text tho as people could inject malicious js into the response.