0

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.

5 Answers 5

1

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

11 Comments

Thanks, we're nearly there, currently the output is <html> <head> <title>PHP Test</title> </head> <body> true </body> </html> I'm trying to find an option to just get the 'true' out, like is possible with .load()
is there a way to set up the php script so it send only the text in response to this request?
You need to remove the HTML tags from your code, and use the header() function to specify that you're sending text (instead of HTML). I've updated my answer.
Thanks, yeah it seems like I need to put the php into it's own file
Oh just realised the html tags aren't necessary at all, I just deleted them all
|
0

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).

Comments

0

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 ?>";

Comments

0

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

1 Comment

Like I said the php is running on a server and the javascript is running locally. They are separate.
0

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.