Total members 11892 |It is currently Sat Oct 12, 2024 12:59 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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;
?>




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  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     -  
 update query example     -  
 vBulletin update     -  
 update an xml file with jaxb     -  
 Get affected rows for an update in php     -  
 Asp.net insert update delete Examples     -  
 Update phpbb to latest version with mods     -  
 update an xml file with jaxb and accessing to its elements     -  
 What is AJAX, How to start AJAX?     -  



Topic Tags

AJAX






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com