Lab Session 13
Lab Session 13
Lab Session 13
Department of CS and SE
Objective
On completion of this lab, students will be able:
AJAX Introduction
Read data from a web server - after the page has loaded
Update a web page without reloading the page
Send data to a web server - in the background
Read data from a web server - after the page has loaded
Update a web page without reloading the page
Send data to a web server - in the background
AJAX updated parts of a web page, without reloading the whole page.
How AJAX Works
Web Engineering
Department of CS and SE
</body>
<script type="text/javascript">
$(document).ready (function () {
$('#submit').click (function (e){
e.preventDefault ();
var name = $("#name").val();
var email = $("#email").val();
var msg_subject = $("#msg_subject").val();
var message = $("#message").val();
$.ajax({
type: "POST",
url: "/formProcess.php",
dataType: "json",
data: {name:name, email:email, msg_subject:msg_subject, message:message},
success : function(data){
if (data.code == "200"){
alert("Success: " +data.msg);
} else {
$(".display-error").html("<ul>"+data.msg+"</ul>");
$(".display-error").css("display","block");
} } }); }); });</script></html>
Form_Procss.php
<?php
$errorMSG = "";
/* NAME */
if (empty($_POST["name"])) {
$errorMSG = "<li>Name is required</<li>";
} else {
$name = $_POST["name"];
}
/* EMAIL */
if (empty($_POST["email"])) {
$errorMSG .= "<li>Email is required</li>";
} else if(!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {
$errorMSG .= "<li>Invalid email format</li>";
}else {
$email = $_POST["email"];
}
/* MSG SUBJECT */
if (empty($_POST["msg_subject"])) {
Web Engineering
Department of CS and SE
Lab Task
In this lab, we learn how to implement AJAX.
requestpage.php file
<?php
echo“Hello World!, AJAX is working on my page”;
?>
ajaxtest.html file
<! DOCTYPE html>
<html> <head> <title>AJAX</title> </head>
<body>
<a href=”javascript:request(‘requestpage.php’, ‘responseDiv’)”>Send
Request</a>
<div id=”responseDiv”></div>
</body> </html>