mcgilljd
asked on
use ajax to query mysql db onchange event from php drop down menu
I want to do an ajax or javascript query of db based on what is select from php based drop down menu.
If user selects fut_sym = abc, then displaythe price in an element like this <td ' id="priceDisplay"></td>
If user selects fut_sym = abc, then displaythe price in an element like this <td ' id="priceDisplay"></td>
This form creates a drop down menu from names in db
<form>
<table width = '980px' border='0px'>
<TD ><select name="fut_sym" onchange="changeSymbol()">
<?php
//query to populate expdate drop down menu on submit form
$query = "SELECT DISTINCT `fut_sym` FROM `dbTable`
WHERE `prod_type` != 'xxx'
ORDER BY `fut_sym`";
$sql = mysql_query("$query");
$num = mysql_num_rows($sql);
$i=0;
while ($i < $num) {
$item = mysql_result($sql,$i,"fut_sym");
$i++;
echo "<option value=\"$item\" ";
if ($fut_sym == $item){ echo " selected ";}
echo ">".$item."</option>"; }
?>
</select>
</TD>
</table>
</form>
<script type="text/javascript">
function changeSymbol()
{
query db select price from dbtable
where fut_sym = symbol from form
var price = query results
document.getElementById('priceDisplay').innerHTML = price;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
What should actions.php look like?
My actions.php look like the same with prices.php in hielo's previous post.
On actions.php, you only write out what you want to display in "priceDisplay" div. Because after calling AJAX function, the HttpRequest object will execute that page, and then, retrieve all rendered text to property HttpRequest.responseText.
On actions.php, you only write out what you want to display in "priceDisplay" div. Because after calling AJAX function, the HttpRequest object will execute that page, and then, retrieve all rendered text to property HttpRequest.responseText.
<?php
//put your db connection info here
$result = mysql_query("select price from dbtable where `fut_sym`='" . mysql_real_escape_string($_REQUEST['fut_sym']) . "'") or die(mysql_error());
if( mysql_num_rows($result) > 0)
{
$row = mysql_fetch_assoc($result);
echo $row['price'];
}
else
{
echo "0.00";
}
exit;
?>
On actions.php, you write out only your query results.
Open in new window