Professional Documents
Culture Documents
1 of 3
http://www.tizag.com/ajaxTutorial/ajax-mysql-database.php
Home
Web Reference
HTML Reference
CSS Reference
CSS Examples
PHP Examples
Web Tutorials
Beginner Tutorial
HTML Tutorial
CSS Tutorial
Scripting
Javascript Tutorial
PHP Tutorial
Perl Tutorial
Ajax Tutorial
ASP Tutorial
VBScript Tutorial
Ajax Tutorial
Ajax
Ajax
Ajax
Ajax
Ajax
Tutorial
Form
Brow ser Support
XMLHttpRequest
PHP
Advanced Ajax
Ajax - MySQL
Ajax - Javascript
Get Help!
Ajax Forum
www.AnyChart.com
SQL Tutorial
MySQL Tutorial
XML Tutorial
Webhost Tutorial
Flash Tutorial
SEO Tutorial
Link to Tizag
Databases
Miscellaneous
Advertise on Tizag.com
Advertise Here
insert all the data rows. The table has four columns:
ae_name - The name of the person
ae_age - Person's age
ae_sex - The gender of the person
ae_wpm - The words per minute that person can type
Contact Us
update order.html
We want to be able to build queries from our HTML file, so there are a few form
elements that will need to be added. The three inputs we are going to implement
are:
Maximum Age (Text Input) - Let the user select the maximum age to be returned.
Maximum WPM (Text Input) - Let the user select the maximum wpm to returned.
Gender (Select Input) - Let the user select the gender of a valid person.
More Tutorials!
Microsoft Office Tutorials
Artist Tutorials
22.02.2012 1:04 PM
2 of 3
http://www.tizag.com/ajaxTutorial/ajax-mysql-database.php
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
document.myForm.time.value = ajaxRequest.responseText;
}
}
var age = document.getElementById('age').value;
var wpm = document.getElementById('wpm').value;
var sex = document.getElementById('sex').value;
var queryString = "?age=" + age + "&wpm=" + wpm + "&sex=" + sex;
ajaxRequest.open("GET", "ajax-example.php" + queryString, true);
ajaxRequest.send(null);
}
//-->
</script>
<form name='myForm'>
Max Age: <input type='text' id='age' /> <br />
Max WPM: <input type='text' id='wpm' />
<br />
Sex: <select id='sex'>
<option>m</option>
<option>f</option>
</select>
<input type='button' onclick='ajaxFunction()' value='Query MySQL' />
</form>
</body>
</html>
If the new Javascript code is foreign to you, be sure to check out our lesson on
Javascript's getElementById Function .
ajax-example.php Code:
<?php
22.02.2012 1:04 PM
3 of 3
http://www.tizag.com/ajaxTutorial/ajax-mysql-database.php
$dbhost = "localhost";
$dbuser = "dbusername";
$dbpass = "dbpassword";
$dbname = "dbname";
//Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
//Select Database
mysql_select_db($dbname) or die(mysql_error());
// Retrieve data from Query String
$age = $_GET['age'];
$sex = $_GET['sex'];
$wpm = $_GET['wpm'];
// Escape User Input to help prevent SQL Injection
$age = mysql_real_escape_string($age);
$sex = mysql_real_escape_string($sex);
$wpm = mysql_real_escape_string($wpm);
//build query
$query = "SELECT * FROM ajax_example WHERE ae_sex = '$sex'";
if(is_numeric($age))
$query .= " AND ae_age <= $age";
if(is_numeric($wpm))
$query .= " AND ae_wpm <= $wpm";
//Execute query
$qry_result = mysql_query($query) or die(mysql_error());
//Build Result String
$display_string = "<table>";
$display_string .= "<tr>";
$display_string .= "<th>Name</th>";
$display_string .= "<th>Age</th>";
$display_string .= "<th>Sex</th>";
$display_string .= "<th>WPM</th>";
$display_string .= "</tr>";
// Insert a new row in the table for each person returned
while($row = mysql_fetch_array($qry_result)){
$display_string .= "<tr>";
$display_string .= "<td>$row[ae_name]</td>";
$display_string .= "<td>$row[ae_age]</td>";
$display_string .= "<td>$row[ae_sex]</td>";
$display_string .= "<td>$row[ae_wpm]</td>";
$display_string .= "</tr>";
}
echo "Query: " . $query . "<br />";
$display_string .= "</table>";
echo $display_string;
?>
DB PHP
Ajax Jquery
PHP Database
Go Back
Ajax Demo
Continue
22.02.2012 1:04 PM