Mike Thornley
Mike Thornley

Reputation: 421

Ajax and PHP requesting a page and showing dynamically

I have an Ajax file I am trying to get to communicate with a PHP file, which will provide the Google home page, into a DIV I have in some HTML of the same page.

I have checked the code and the PHP file thoroughly and get no errors, yet I can't understand why nothings happening. Can someone please help me?. Thanks in advance.

Here's my ajax

<html>
  <head>
    <title>Ajax page</title>
  </head>
    <body> 

<h1>AJAX page</h1>
  <div id="info">
    This content will be changed by default....
  </div>

<script type="text/javascript">

params = "url=google.com";
request = new ajaxRequest();
request.open("POST", "ajaxLab.php", true);

request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.setRequestHeader("Content-length", params.length);
request.setRequestHeader("Connection", "close");

request.onreadystatechange = function(){
  if(this.readystate==4){
    if(this.status==200){
      if(this.responseText != null) document.getElementById("info").innerHTML=this.responseText;
      else alert("Ajax error: No data received ");
    }
    else alert("Ajax error: " + this.statusText);
  }
}
request.send(params);

function ajaxRequest()
{
  try
  {
  var request = new XMLHttpRequest();
  }
  catch(e1)
  {
    try
    {
    request = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e2)
    {
      try
      {
      request = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e3)
      {
      request=false;
      }
    }
  }
return request; 
} 

</script>

  </body>
</html>

Here's my simple php file..

<?php

if(isset($_POST['url'])){
echo file_get_contents("http://".sanitiseVar($_POST['url']));
}

function sanitiseVar($var){
    $var = htmlentities($var);
    $var = stripslashes($var);
    return strip_tags($var);
}

?>

Upvotes: 1

Views: 230

Answers (3)

Mike Thornley
Mike Thornley

Reputation: 421

Ok, readyState property wasn't bumpycapped. Worked after I corrected that!.

Upvotes: 1

Dzhuneyt
Dzhuneyt

Reputation: 8701

Use this:

$(document).ready(function(){
    $.post("ajaxLab.php",
           {
                url:"google.com",
                // another_param : "its value"
           },
           function(result){ // Callback function, on success
             if(result != null)
                 $('#info').html(result);
             else
                alert("Ajax error: No data received ");
          }
    );    
});

Remember to include the jQuery library in the header:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>

Upvotes: 0

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167162

Change the whole file this way:

<html>
    <head>
        <title>Ajax page</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $("#info").load("ajaxLab.php", {'url': 'google.com'});
            });
        </script>
    </head>
    <body> 
        <h1>AJAX page</h1>
        <div id="info">
            This content will be changed by default....
        </div>
    </body>
</html>

Upvotes: 0

Related Questions