...

View Full Version : Shorten my Ajax Code



emjez15
12-08-2010, 04:58 PM
I would just like to shorten my httprequest code so I will not create 3 variables(xmlhttp) & 3 PHP files.

Here's my ajax Code:

function showStudInfo(str)
{
if(window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
xmlhttp2 = new XMLHttpRequest();
xmlhttp3 = new XMLHttpRequest();
}
else
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp2 = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp3 = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.form1.fname.value = xmlhttp.responseText;
}
}
xmlhttp2.onreadystatechange=function()
{
if(xmlhttp2.readyState==4 && xmlhttp2.status==200)
{
document.form1.lname.value = xmlhttp2.responseText;
}
}
xmlhttp3.onreadystatechange=function()
{
if(xmlhttp3.readyState==4 && xmlhttp3.status==200)
{
document.form1.course.value = xmlhttp3.responseText;
}
}
xmlhttp.open("GET","php/incident-showfname.php?studnum="+str,true);
xmlhttp2.open("GET","php/incident-showlname.php?studnum="+str,true);
xmlhttp3.open("GET","php/incident-showcourse.php?studnum="+str,true);
xmlhttp.send();
xmlhttp2.send();
xmlhttp3.send();
}

here's my PHP Code:

<?php
$q=$_GET["studnum"];

$host= // Host name
$username= // Mysql username
$password= // Mysql password
$db_name= // Database name
$tbl_name= // Table name
$con = mysql_connect($host, $username, $password)or die("cannot connect server ");

mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM studentinfo" or die("cannot connect server ");

$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
if ($row['studnum'] == $q)
{
echo $row['fname'];
}
}
mysql_close($con); ?>

Thanks & God Bless



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum