Switch to full style
JavaScript code examples
Post a reply

Update table with search box using ajax

Sun Jun 10, 2012 11:50 pm

In this code we search in table using ajax and php snippet.
Code:


<html>
<
body>

<
script language="javascript" type="text/javascript">
 
function 
upDateUsingAjax(){
    var 
ajaxRequest;  // ajax object will be stored in it
    
    
try{
     
        
ajaxRequest = new XMLHttpRequest();
    } catch (
e){
     
        try{
            
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (
e) {
            try{
                
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (
e){
             
                
alert("Error");
                return 
false;
            }
        }
    }
    
// recieve data sent from user
    
ajaxRequest.onreadystatechange = function(){
        if(
ajaxRequest.readyState == 4){
            
document.myForm.time.value ajaxRequest.responseText;
        }
    }
    var 
age document.getElementById('age').value;
    var 
Address document.getElementById('Address').value;
    var 
gender document.getElementById('v').value;
    var 
queryString "?age=" age "&Address=" Address "&gender=" gender;
    
ajaxRequest.open("GET""ajax-example.php" queryStringtrue);
    
ajaxRequest.send(null); 
}

 
</script>



<form name='myForm'>
 Age: <input type='text' id='age' /> <br />
 Address: <input type='text' id='Address' />
<br />
Gender: <select id='gender'>
<option>Male</option>
<option>Female</option>
</select>
<input type='button' onclick='upDateUsingAjax()' value='Query MySQL' />
</form>
</body>
</html>



Server side PHP snippet :
Code:



<?php
$dbhost 
"dbhostURL";
$dbuser "dbUsername";
$dbpass "dbpassword";
$dbname "dbname";
    
//open connection to Mysql
mysql_connect($dbhost$dbuser$dbpass);
    
//choose database
mysql_select_db($dbname) or die(mysql_error());
    
// Retrieve data from Query String
$age $_GET['age'];
$gender $_GET['gender'];
$address $_GET['address'];
    
// Escape User Input to help prevent SQL Injection
$age mysql_real_escape_string($age);
$gender mysql_real_escape_string($gender);
$address mysql_real_escape_string($address);
    
//build query
$query "SELECT * FROM ajax_example WHERE ae_gender = '$gender'";
 
    
//Execute query
$qry_result mysql_query($query) or die(mysql_error());

    
//Build Result String
$printString "<table>";
$printString .= "<tr>";
$printString .= "<th>Name</th>";
$printString .= "<th>Age</th>";
$printString .= "<th>gender</th>";
$printString .= "<th>address</th>";
$printString .= "</tr>";

    
// Updated table
while($row mysql_fetch_array($qry_result)){
    
$printString .= "<tr>";
    
$printString .= "<td>$row[name]</td>";
    
$printString .= "<td>$row[age]</td>";
    
$printString .= "<td>$row[gender]</td>";
    
$printString .= "<td>$row[address]</td>";
    
$printString .= "</tr>";
    
}
 
$printString .= "</table>";
echo 
$printString;
?>




Post a reply
  Related Posts  to : Update table with search box using ajax
 A simple search page using Google AJAX Search API     -  
 Load file and update content using AJAX and JQuery     -  
 vBulletin update     -  
 update query example     -  
 Get affected rows for an update in php     -  
 update an xml file with jaxb     -  
 Asp.net insert update delete Examples     -  
 update an xml file with jaxb and accessing to its elements     -  
 Update phpbb to latest version with mods     -  
 What is AJAX, How to start AJAX?     -  

Topic Tags

AJAX