Skip to content

Asynchronous Javascript And XML

October 26, 2006

Жишээ үзнэ үү. 

<html>
<body>

<script language=”javascript” type=”text/javascript”>
<!–
//Browser Support Code
function ajaxFunction(){
var ajaxRequest;  // The variable that makes Ajax possible!

try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject(“Msxml2.XMLHTTP”);
} catch (e) {
try{
ajaxRequest = new ActiveXObject(“Microsoft.XMLHTTP”);
} catch (e){
// Something went wrong
alert(“Your browser broke!”);
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>

<?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; ?>
2 Comments leave one →
  1. November 1, 2006 12:40 pm

    saasdddddddddddddd

  2. June 12, 2007 7:47 am

    анхны ойлголт авахад зүгээр л юм байна. Ахиад AJAX жишээ илүү их тавиарай

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: