Home  • Programming • JQuery

Examples use of jQuery ajax() Method

Syntax $.ajax({name:value, name:value, ... })

Example by post data

PHP search.php
<?php  
 
  if(isset($_POST["txtSearch"])){

     $search_word=$_POST["txtSearch"];
     
     $db=new mysqli("localhost","root","","test");

     $result=$db->query("select id,username,email,password from user where username like '%$search_word%' ");
    
     echo "<table><tr><th>ID</th><th>Name</th><th>Email</th></tr>";

     while(list($id,$name,$email)=$result->fetch_row()){   
        
         echo "<tr><td>$id</td><td>$name</td><td>$email</td></tr>";        

      }//end loop
     echo "</table>";

  }//end if

?>
HTML and JQuery ajax.htm
<!DOCTYPE html>
<html>
<head>
    <title>JQuery $.ajax() example</title>
    <script src="jquery-1.5.min.js" type="text/javascript"></script> 

<script>

$(function(){	 
    $("#btnSearch").click(function(){
     
    var keyword=$("#txtSearch").val();
    $("#output").html("Loading...");
	
    $.ajax({
		url:"search.php",
		type:"post",
		data:{txtSearch:keyword},
		success: function(result){
		  $("#output").html(result);
		}//end success
     
    });//end ajax method
     
    });//end click event
 });//end ready dom 

</script>

</head>
<body>
[code]
Search: <input type="text" id="txtSearch" />
        <input type="button" id="btnSearch" value="Search" >
<br/>
<div id="output"></div>
</body>
</html>
$.ajax() configure parameters name/value <tbody> </tbody>
Name Value/Description
async A Boolean value indicating whether the request should be handled asynchronous or not. Default is true
beforeSend([em]xhr[/em]) A function to run before the request is sent
cache A Boolean value indicating whether the browser should cache the requested pages. Default is true
complete([em]xhr,status[/em]) A function to run when the request is finished (after success and error functions)
contentType The content type used when sending data to the server. Default is: &quot;application/x-www-form-urlencoded&quot;
context Specifies the &quot;this&quot; value for all AJAX related callback functions
data Specifies data to be sent to the server
dataFilter([em]data[/em],[em]type[/em]) A function used to handle the raw response data of the XMLHttpRequest
dataType The data type expected of the server response.
error([em]xhr,status,error[/em]) A function to run if the request fails.
global A Boolean value specifying whether or not to trigger global AJAX event handles for the request. Default is true
ifModified A Boolean value specifying whether a request is only successful if the response has changed since the last request. Default is: false.
jsonp A string overriding the callback function in a jsonp request
jsonpCallback Specifies a name for the callback function in a jsonp request
password Specifies a password to be used in an HTTP access authentication request.
processData A Boolean value specifying whether or not data sent with the request should be transformed into a query string. Default is true
scriptCharset Specifies the charset for the request
success([em]result,status,xhr[/em]) A function to be run when the request succeeds
timeout The local timeout (in milliseconds) for the request
traditional A Boolean value specifying whether or not to use the traditional style of param serialization
type Specifies the type of request. (GET or POST)
url Specifies the URL to send the request to. Default is the current page
username Specifies a username to be used in an HTTP access authentication request
xhr A function used for creating the XMLHttpRequest object

Comments 0


Copyright © 2024. Powered by Intellect Software Ltd