Menu
Hi,
I am trying to implement AJAX to my forms. I have one dropdown field which is being populated by a backend $mysql_query. I want to click a button “Go” and the selected value returns the row of data via a mysql_fetch. I am following [URL=”http://www.w3schools.com/php/php_ajax_database.asp”]this example
[CODE]<select name="myfield" id="myfield">
<?php
$host="Host name "
$username="Mysql username ";
$password="Mysql password ";
$db_name="Database name ";$tbl_name="Table name";
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
//dropdown field for Shipping Profile
$q = mysql_query("SELECT * FROM $tbl_name WHERE key='$key'");
//build the field
echo "<select name ='myfield'>"
;while($r = mysql_fetch_assoc($q)) {echo '<option value="'.$r['ColValue'].'">'.$r['ColValue'].'</option>';}echo '</select>';mysql_close();?>
</select>
<input name="Go" type="button" id="Go" value="Go" onclick="showUser(this.value)"></td>
//this is part of the .js file (based on the example-it's where I get lost)
function showUser(str) <br />
{ <br />
xmlhttp=GetXmlHttpObject(); <br />
if (xmlhttp==null) <br />
{ <br />
alert ("Browser does not support HTTP Request"); <br />
return; <br />
} <br />
var url="get_location.php"; <br />
url=url+"?q="+str; <br />
url=url+"&sid="+Math.random(); <br />
xmlhttp.onreadystatechange=stateChanged; <br />
xmlhttp.open("GET",url,true); <br />
xmlhttp.send(null); <br />
}
I am just trying to populate form fields based on the mysql_query but the .js function is not working for me. Any suggestions?
[/CODE]
[CODE] while($row=mysql_fetch_array($result)) {
$return[] = $row;
}
return $return;
}
?>
[/CODE]
[CODE]document.getElementById("txtHint").innerHTML=xmlhttp.responseText;[/CODE]
[code=html]
<td id="txtHint"></td>[/code]
[code=html]
<td id="txtHint">
<table border='1'>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>
<tr>
<td>First Name Data</td>
<td>Last Name Data</td>
<td>Age Data</td>
<td>Hometown Data</td>
<td>Job Data</td>
</table>
</td>
[/code]
[code=php]
echo "<ul>";
echo "<li>" . $row['FirstName'] . "</li>";
echo "<li>" . $row['LastName'] . "</li>";
echo "<li>" . $row['Age'] . "</li>";
echo "<li>" . $row['Hometown'] . "</li>";
echo "<li>" . $row['Job'] . "</li>";
}
echo "</ul>";
[/code]
[code=html]//dropdown field for Shipping Profile
$q = mysql_query("SELECT * FROM $tbl_name WHERE hf_AcctNo='$AcctNo'");echo "<select name ='select_ship'>"
;while($r = mysql_fetch_assoc($q)) {echo '<option value="'.$r['ShipAddress'].'">'.$r['ShipAddress'].'</option>';}echo '</select>';mysql_close();?>
</select>
<input name="Go" type="button" id="Go" value="Go" onclick="showUser(document.shipform.select_ship.value)"></td>
</tr>
<tr>
<td>Ship To Name: </td>
<td><input name="ShipName" type="text" id="ShipName" value="<?php echo $ShipName?>" /></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Address: </td>
<td><input name="ShipAddress" type="text" id="ShipAddress" value="<?php echo $ShipAddress?>" /></td>
<td>Address: (line 2) </td>
<td><input name="ShipAddress2" type="text" id="ShipAddress2" value="<?php echo $ShipAddress2?>" /></td>
</tr>
</form>[/code]
[CODE]<?php
$q=$_GET['q'];
$host=Host name
$username=Mysql username
$password=Mysql password
$db_name=Database name
$tbl_name=Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name WHERE ShipAddress='".$q."'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>ShipName</th>
<th>ShipAddress</th>
<th>ShipAddress2</th>
<th>ShipCity</th>
<th>ShipState</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ShipName'] . "</td>";
echo "<td>" . $row['ShipAddress'] . "</td>";
echo "<td>" . $row['ShipAddress2'] . "</td>";
echo "<td>" . $row['ShipCity'] . "</td>";
echo "<td>" . $row['ShipState'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close();
?>
[/CODE]
[code=html]<select name="users" onchange="showUser(this.value)">[/code]
[code=html]
<form >
Query by Address:
<input id="ShipAddress" name="ShipAddress" type="text" size="20" value="" onblur="showAddress(this.value)"/>
<input id="GetAddress" name="GetAddress" type="button" value="Search" />
</form>
[/code]
[code=html]
function showAddress(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="getaddress.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
[/code]
[code=php]
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'User', 'password');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database_name", $con);
$sql="SELECT * FROM $tbl_name WHERE ShipAddress LIKE '".$q."'"; // allows upper/lower case differences
//$sql="SELECT * FROM $tbl_name WHERE ShipAddress LIKE '%".$q."%'"; // allows partial match
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>ShipName</th>
<th>ShipAddress</th>
<th>ShipAddress2</th>
<th>ShipCity</th>
<th>ShipState</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ShipName'] . "</td>";
echo "<td>" . $row['ShipAddress'] . "</td>";
echo "<td>" . $row['ShipAddress2'] . "</td>";
echo "<td>" . $row['ShipCity'] . "</td>";
echo "<td>" . $row['ShipState'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close();
?>
[/code]
0.1.9 — BETA 6.17