Sun Jun 10, 2012 11:50 pm
<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" + queryString, true);
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>
<?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;
?>
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
Powered by phpBB © phpBB Group.