I had a form with 2 dropdownlist one select airport and the other select zipcode. I need to get the price from mysql based on user selection.
when I used one dropdownlist I can get the data. my problem is how to pass the 2 dropdownlist variable to get the only the price from the table, based on user selection from airport drop and zip
My table goes like this
ID : Airport : ZipCode : Price
1 : Dal : 75062 :70
2 : Dal : 75061 :80
3 : DFW : 75062 :65
4 : DFW : 75062 :80
the following code work fine with one drop down list its just work from the airport dropdown, I need to include the second one in the query without submitting the form.
Just add an AND logical operator to your WHERE clause.
Code:
. . . WHERE airport = 'foo' AND zipcode = 'bar'
PS: You should probably look at mysql_real_escape_string() in order to avoid SQL injection issues.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Many thanks for your response and help. But that didn't work for me. I have two dropdown element of for airport and other for zip code I need the price to appear in the same page before submitting the form.
You have, at first, to send the two selected values with your Ajax function showUser(str) which can not now to be call with an unique argument str !
After reading the two values (for example, var airportSlc=document.form[0].users.value; for the first), this function without argument (called with an onsubmit button) as to make tests and finally to send a get with the two values with a complete get url such as the following one...
Additionally, add a DOCTYPE at your page, change users with airport, remove the useless upper case letter in event (for example onchange="getPrice()" on the submit button) and add values to the zip drop-down list options.
/*
$con = mysql_connect('localhost', 'root', '');
if (!$con) die('Could not connect: ' . mysql_error());
$dbs = mysql_select_db("ejbooking", $con);
if (!$dbs) die('Can\'t use ejbooking : ' . mysql_error());
$sql = 'SELECT price FROM price WHERE zipcode = "'.$zip.'" and airport= "'.$arp.'"';
$result = mysql_query($sql);
echo "<table border='1'>
...
echo "</table>";
mysql_close($con);
*/
// Only for test
echo "The server received effectively $arp and zipcode $zip !";
}
else echo "No GET request on the server !";
?>
Ajax.htm and get_user.php you provide are not working nothing can be pulled out from the database, I remove the comments from the query section and nothing happened.
I tried to pass the function onsubmit too but its not working.
I have only rewrite the sql request with the php variables $zip and $arp, but was not able to test it without database... Both pages work perfectly with the comments. It should be easy to finalize the request with the alert(xmlhttp.responseText) witch display the PHP or SQL error messages if any.
The button on submit is useless with Ajax, it is now only useful to see the form of the GET url.
Good Luck !
Thank you,
I am still can not test it. when I test it with comments it work fine, when using database its not working. you can find attached database in earlier post.
Hi,
Thank you so much for your help, The problem now solved. I made minor change to the php code specially the query, here is the code working perfect with your Ajax file.
Code:
<?php
$arp=$_GET["arp"];
$zip=$_GET["zip"];
$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('ejbooking', $con);
if (!$db_selected) {
die ('Can\'t use DB : ' . mysql_error());
}
$sql="SELECT `price` FROM `price` WHERE airport = '".$arp."' AND zipcode=".$zip." ;";
$result = mysql_query($sql);
if(!$result)
{
die('check your query :'.mysql_error());
}
while($row = mysql_fetch_array($result)) {
echo $row['price'] ;
}
mysql_close($con);
?>
I am still working in the form I hope I can finalize the rest of it.
Bookmarks