-1

I have a php script that returns an echo of 'Hello' when accessed half the time (just based on a random number).

Is it possible to make a javascript script on a HTML page that will:

Go to the the PHP page,

If 'Hello' is returned it will show an alert of 'Goodbye'

ELSE

It shows 'nothing found'

What do I need to do this?

4 Answers 4

1

That is called AJAX. Example:

var msg = 'Nothing found';
var xhr = new XMLHttpRequest();

xhr.onreadystatechange = function() {
  if (xhr.readyState == 4 && xhr.status == 200) {
    alert(xhr.responseText != "Hello" ? msg : "Goodbye");
  }
};

xhr.open('GET', 'http://example.com', true);
xhr.send(null);

See also:
https://developer.mozilla.org/en/AJAX/Getting_Started

Sign up to request clarification or add additional context in comments.

Comments

1

Best would be to use jQuery and Ajax to fetch the page.

       $.ajax({ 
       type:"GET", 
       url:"file.php", 
       data:id, // if you want to pass a param, optional
       success:function(data){ 
          alert(data);//do something with data
       }});

Comments

0

Since the information is not very confidential, I can suggest you to use Cookies (in case you don't want to use AJAX, but I still recommend you to use AJAX). People don't usually take the advantage of Cookies.

So, in PHP file, set the Cookies with your information:

$value = 'Hello';
setcookie("Information", $value);

And get it from HTML using Javascript (from W3School)

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

Comments

0

You could also just include the page in an iframe (with or without a meta-refresh) and instead of echoing 'hello' you would echo the javascript alert ;)

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.