Sei sulla pagina 1di 23

PHP and AJAX

Prepared by T.C Jayanidu Wattega


ma

XMLHttpRequest object, a web page can


make a request to, and get a response from a
web server - without reloading the page.

Prepared by T.C Jayanidu

onreadystatechange
Property

The XMLHttpRequest object has a special


property called onreadystatechange.
This property, onreadystatechange, stores a
function
onreadystatechange stores the function that
will process the response from the server.

Prepared by T.C Jayanidu

As the name sort of implies, every time the


"ready state" changes this function will be
executed

Prepared by T.C Jayanidu

/ /Create a function that will receive data sent


from the server
ajaxRequest.onreadystatechange =
function(){
// code here
}

Prepared by T.C Jayanidu

readyState Property

The XMLHttpRequest object has another


property called readyState.
This is where the status of our server's
response is stored .
The response can be processing,
downloading or completed.

Prepared by T.C Jayanidu

Each time the readyState changes then our


onreadystatechange function executes.
When the property readyState is 4 that
means the response is complete and we can
get our data.

Prepared by T.C Jayanidu

// Create a function that will receive data sent


from the server
ajaxRequest.onreadystatechange = function()
{ if(ajaxRequest.readyState == 4){ // Get the
data from the server's response } }

Prepared by T.C Jayanidu

we can access the property that stores the


server's response, responseText .

Prepared by T.C Jayanidu

responseText Property
// Create a function that will receive data sent
from the server
ajaxRequest.onreadystatechange = function()
{ if(ajaxRequest.readyState == 4)
{ document.myForm.time.value =
ajaxRequest.responseText;
}
}
Prepared by T.C Jayanidu

Sending a Request for


Information

1.

2.

Sending a request is comprised of two


steps:
Specify the URL of server-side script that
will be used in our Ajax application .
Use the send function to send off the
request.

Prepared by T.C Jayanidu

The URL is set using the open method, which


takes three arguments.
The second argument is the important one,
as it is the URL of our PHP script.

Prepared by T.C Jayanidu

// Create a function that will receive data sent


from the server
ajaxRequest.onreadystatechange = function()
{ if(ajaxRequest.readyState == 4)
{
document.FormName.InputName.value =
ajaxRequest.responseText;
}
} ajaxRequest.open("GET", "server.php", true);
Prepared by T.C Jayanidu

<html>
<body>
<script language="javascript
type="text/javascript">
function ajaxFunction()
{
var ajaxRequest; // The variable that makes
Ajax possible!

Prepared by T.C Jayanidu

// Create a function that will receive data sent from the


server ajaxRequest.onreadystatechange = function()
{
if(ajaxRequest.readyState == 4)
{
document.myForm.time.value =
ajaxRequest.responseText;
}
} ajaxRequest.open("GET", "serverTime.php", true);
ajaxRequest.send(null);
}
</script>
Prepared by T.C Jayanidu

<form name='myForm'> Name: <input


type='text' onChange="ajaxFunction();"
name='username' /> <br /> Time: <input
type='text' name='time' />
</form>
</body>
</html>

Prepared by T.C Jayanidu

<?php
echo date("H:i:s");
?>

Prepared by T.C Jayanidu

Database search
<html>
<head>
<script type="text/javascript" src=esoft.js"></script>
</head>
<body>
<form>
Select a course :
<select name=course" onchange="show(this.value)">
<option value="1">BCS</option>
<option value="2">BIT </option>
<option value="3">HND </option>
<option value="4">Web Development</option>
</select>
</form>
<br />
<div id=Details"><b>Details for every course will display below</b></div>
</body>
</html>

Prepared by T.C Jayanidu

var xmlhttp;
function show(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url=course.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

Prepared by T.C Jayanidu

function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById(" Details
").innerHTML=xmlhttp.responseText;
}
}
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
Prepared by T.C Jayanidu

<?php
$course=$_GET["q"];
$con = mysql_connect('localhost', root', root');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db(Esoft", $con);
$sql="SELECT * course WHERE id = ". $course."'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>Course</th>
<th>Std Name</th>
<th>Contact Number</th>
<th>Address</th>
<th>status</th>
</tr>";

Prepared by T.C Jayanidu

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row[course'] . "</td>";
echo "<td>" . $row[StdName'] . "</td>";
echo "<td>" . $row[ContactNumber'] . "</td>";
echo "<td>" . $row[Adress'] . "</td>";
echo "<td>" . $row[Status'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>

Prepared by T.C Jayanidu

Prepared by T.C Jayanidu

Potrebbero piacerti anche