0% found this document useful (0 votes)
45 views3 pages

Ajax Code

The document consists of two parts: a PHP script (urlpost.php) that fetches the content of a specified URL after sanitizing the input, and an HTML file (urlpost.html) that demonstrates an AJAX request to the PHP script. The HTML file includes a JavaScript function to send a POST request with a URL parameter and update a DIV with the fetched content. The AJAX implementation handles different browser compatibility for XMLHttpRequest.

Uploaded by

kaushiksubs000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views3 pages

Ajax Code

The document consists of two parts: a PHP script (urlpost.php) that fetches the content of a specified URL after sanitizing the input, and an HTML file (urlpost.html) that demonstrates an AJAX request to the PHP script. The HTML file includes a JavaScript function to send a POST request with a URL parameter and update a DIV with the fetched content. The AJAX implementation handles different browser compatibility for XMLHttpRequest.

Uploaded by

kaushiksubs000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

urlpost.

php

<?php // urlpost.php

if (isset($_POST['url']))

echo file_get_contents('http://' . SanitizeString($_POST['url']));

function SanitizeString($var)

$var = strip_tags($var);

$var = htmlentities($var);

return stripslashes($var);

?>

urlpost.html

<!DOCTYPE html>

<html>

<head>

<title>AJAX Example</title>

</head>

<body style='text-align:center'>

<h1>Loading a web page into a DIV</h1>

<div id='info'>This sentence will be replaced</div>

<script>

params = "url=amazon.com/gp/aw"

request = new ajaxRequest()

request.open("POST", "urlpost.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>

You might also like