Professional Documents
Culture Documents
Ajaxapplicaion Creating
Ajaxapplicaion Creating
Applications
Key Skills & Concepts
</head>
<body>
<H1>An Ajax example</H1>
<div id="targetDiv">
<p>The fetched message will appear here.</p>
</div>
</body>
</html>
Adding button
<form>
<input type = "button" value = "Fetch the
message"
onclick = "getData('data.txt', 'targetDiv')">
</form>
}
}
Adding Some Server-Side
Programming
• To do a little programming on the server,
we’re going to use PHP, the server-side
language.
• Create data.php(and add to folder instead of
data.txt) a PHP script that will send a message
back to the browser.
• To follow along here, you to need a
PHP-enabled server
• We start data.php
<?php
echo 'This text comes to you thanks to PHP.';
?>
The function call is as below:
<form>
<input type = "button" value = "Fetch the
message"
onclick = "getData('data.php', 'targetDiv')">
</form>
Sending Data to the Server Using GET
• The GET method sends text data to the server
by appending that text to the end of the URL
• Example: We have an argument “name,” and
assign value “Steve” to it
• http://www.server.com/script.php?name=Stev
e
*question mark is used to separate your data
from the navigation part of the URL
Lets design application
Writing the PHP
• dataresponder.php
<?php
if ($_GET["data"] == "1") {
echo 'The server got a value of 1';
}
if ($_GET["data"] == "2") {
echo 'The server got a value of 2';
}
?>
Interacting with dataresponder.php
• Let’s write the Ajax-enabled page, ajax3.html,
that interacts with dataresponder.php.
• We’re going to need two buttons here: one to
send a value of 1 to dataresponder.php, and
the other to send a value of 2.
AJAX enabled form
<h1>Sending Data to the Server</h1>
<form>
<input type = "button" value = "Fetch message 1"
onclick = "getData('dataresponder.php?data=1',
'targetDiv')">
<input type = "button" value = "Fetch message 2"
onclick = "getData('dataresponder.php?data=2',
'targetDiv')">
</form>
Sending Data to the Server Using
POST
• The POST method offers more security,
because the data you send to the server is not
URL-encoded
Writing the PHP (POST method)
• The $_POST array used in this PHP script,
dataresponderpost.php:
<?php
if ($_POST["data"] == "1") {
echo 'You sent the server a value of 1';
}
if ($_POST["data"] == "2") {
echo 'You sent the server a value of 2';
}
?>
sending data via POST
• To enable sending data via POST, have to set
an HTTP request header
• Content-Type, to
"application/x-www-form-urlencoded".
function getData(dataSource, divID)
{
if(XMLHttpRequestObject) {
var obj = document.getElementById(divID);
XMLHttpRequestObject.open("POST", dataSource);
XMLHttpRequestObject.setRequestHeader('Conten
t-Type',
'application/x-www-form-urlencoded');
.
.
.
}
The form
• <form>
• <input type = "button" value = "Fetch message
1"
• onclick = "getData('dataresponder.php?data=1',
'targetDiv')">
• <input type = "button" value = "Fetch message
2"
• onclick = "getData('dataresponder.php?data=2',
'targetDiv')">
• </form>
Send method
• Now, however, instead of sending a value of
null, you send the data you want to send via
POST in a URL-encoded string.
• In this case, that string is "data=1" or "data=2"
depending on what the getData function’s
data argument holds.
• So we can send our data to the server in this
way, using the XMLHttpRequest object’s send
method:
XMLHttpRequestObject.send("data=" + data);