0% found this document useful (0 votes)
20 views17 pages

Lecture 6 FS

Uploaded by

๖OxY乛 RÆZOR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views17 pages

Lecture 6 FS

Uploaded by

๖OxY乛 RÆZOR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Full Stack Development

(IT432)

Anju Mishra
Department of Information Technology

1
Outline
 AJAX
 Load()
 Get()
 Post()

 Form Validation

2
jQuery - AJAX
 AJAX = Asynchronous JavaScript and XML.

 AJAX is about loading data in the background and display it on the webpage,
without reloading the whole page.

 Examples of applications using AJAX: Gmail, Google Maps, Youtube, and Facebook
tabs. jQuery provides several methods for AJAX functionality.

 With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from
a remote server using both HTTP Get and HTTP Post - And you can load the
external data directly into the selected HTML elements of your web page!

3
AJAX load() Method
 The load() method loads data from a server and puts the returned data into the
selected element.

Syntax: $(selector).load(URL, data, callback);

 The required URL parameter specifies the URL you wish to load.

 The optional data parameter specifies a set of query string key/value pairs to send
along with the request.

 The optional callback parameter is the name of a function to be executed after the
load() method is completed.
4
 It is also possible to add a jQuery selector to the URL parameter.

 The following example loads the content of the element with id="p1",
inside the file "demo_test.txt", into a specific <div> element:

Example
$("#div1").load("demo_test.txt #p1");

5
Exmaple

6
Exmaple

7
 The optional callback parameter specifies a callback function to run when the load()
method is completed. The callback function can have different parameters:

 responseTxt - contains the resulting content if the call succeeds


 statusTxt - contains the status of the call
 xhr - contains the XMLHttpRequest object

 The following example displays an alert box after the load() method completes. If the
load() method has succeeded, it displays "External content loaded successfully!", and
if it fails it displays an error message:

8
9
10
jQuery $.get() Method
The $.get() method requests data from the server with an HTTP GET request.
Syntax:
$.get(URL,callback);

The required URL parameter specifies the URL you wish to request.
The optional callback parameter is the name of a function to be executed if the request
succeeds.
The following example uses the $.get() method to retrieve data from a file on the server:
Example
$("button").click(function(){
$.get("demo_test.asp", function(data, status){
alert("Data: " + data + "\nStatus: " + status);
});
}); 11
The first parameter of $.get() is the URL we wish to request ("demo_test.asp").

The second parameter is a callback function. The first callback parameter holds the content
of the page requested, and the second callback parameter holds the status of the request.

Here is how the ASP file looks like ("demo_test.asp"):

<%
response.write("This is some text from an external ASP file.")
%>
12
jQuery $.get() Example

13
jQuery $.post() Method
The $.post() method requests data from the server using an HTTP POST request.

Syntax: $.post(URL,data,callback);

The required URL parameter specifies the URL you wish to request.
The optional data parameter specifies some data to send along with the request.
The optional callback parameter is the name of a function to be executed if the request
succeeds.
Example
$("button").click(function(){
$.post("demo_test_post.asp",
{
name: “Anil", city: “Noida"
},
function(data, status){
alert("Data: " + data + "\nStatus: " + status);
});
}); 14
The first parameter of $.post() is the URL we wish to request ("demo_test_post.asp").
Then we pass in some data to send along with the request (name and city).
The ASP script in "demo_test_post.asp" reads the parameters, processes them, and returns a result.
The third parameter is a callback function. The first callback parameter holds the content of the page
requested, and the second callback parameter holds the status of the request.

Here is how the ASP file looks like ("demo_test_post.asp"):

<%
dim fname,city
fname=Request.Form("name")
city=Request.Form("city")
Response.Write("Dear " & fname & ". ")
Response.Write("Hope you live well in " & city & ".")
%>

15
Exmaple

16
Form Validation using jQuery
 We use the jQuery Validation Plugin to validate our form. The basic
principle of this plugin is to specify validation rules and error messages for
HTML elements in JavaScript.

 Step 1: Include jQuery


 Step 2: Include the jQuery Validation Plugin
 Step 3: Create the HTML Form
 Step 4: Create Styles for the Form
 Step 5: Create the Validation Rules

17

You might also like