Populating a list from a MySQL query
Moderator: General Moderators
Populating a list from a MySQL query
I am trying to populate one drop down list based on the contents of another list. When a user selects an item from the first list, I want to run a MySQL query based on the value of that item, then populate the second list from the results of the query.
I realize that PHP is server-side, and that this will require the page to be reloaded, but as long as the functionality is as I described, then its okay. The main issues are having the second list populate automatically once an item from the first list is selected and to populate the second list with a MySQL query using PHP.
Anyone have any ideas as to how this can be accomplished? Any help at all would be appreciated.
I realize that PHP is server-side, and that this will require the page to be reloaded, but as long as the functionality is as I described, then its okay. The main issues are having the second list populate automatically once an item from the first list is selected and to populate the second list with a MySQL query using PHP.
Anyone have any ideas as to how this can be accomplished? Any help at all would be appreciated.
I don't know anything about DOM so that probably won't work for me.
I suppose I could just have a Javascript function call the page again with a variable in the query string which PHP could use to populate the second list. I'd like to avoid reloads as much as possible though.
If I pre-store the results of each possible query (there would only be a handfull to store), I'm still not sure how I can use Javascript to choose the correct set of results. Can I just stick a PHP block in a Javascript function?
I suppose I could just have a Javascript function call the page again with a variable in the query string which PHP could use to populate the second list. I'd like to avoid reloads as much as possible though.
If I pre-store the results of each possible query (there would only be a handfull to store), I'm still not sure how I can use Javascript to choose the correct set of results. Can I just stick a PHP block in a Javascript function?
DOM = document.object
in other words, it is javascript.
if you want to reduce number of reloads, then you are on the 2nd way
here is an example
and the javascript part goes here 
if you want to reduce number of reloads, then you are on the 2nd way
here is an example
Code: Select all
<form action="prices.php" method="post" name="calcit">
<table cellpadding="2" cellspacing="2" align="center">
<tr><td colspan="3"><img src="images/spacer.gif" height="5"></td></tr>
<tr><td>KALKIÞ</td><td>:</td><td>
<select name="start" onchange="change()" style="width: 190px">
<option selected value="xnull">Kalkýþ için bir yer seçiniz.</option>
<option value="kosekoy">Ýzmit (Köseköy)</option>
<option value="afyon">Afyon</option>
<option value="sivas">Sivas</option>
</select>
</td></tr>
<tr><td>VARIÞ</td><td>:</td><td align="left">
<select name="finish" style="width: 190px">
<option selected value="xnull">Varýþ için bir yer seçiniz.</option>
</select>
</td></tr>
<tr><td>AÐIRLIK (ton)</td><td>:</td><td><input type="text" value="0" name="ton" size="14"></td></tr>
<tr><td>ÜCRET (TL)</td><td>:</td><td><input type="text" value="0,000" name="ton" size="14" disabled></td></tr>
<tr><td colspan="3"><img src="images/spacer.gif" height="3"></td></tr>
<tr><td colspan="3" align="center"><input type="submit" name="Hesapla" value="Hesapla" class="xinput"></td></tr>
<tr><td colspan="3"><img src="images/spacer.gif" height="5"></td></tr>
</table>
</form>Code: Select all
<script language="javascript">
var arFieldText1=new Array("Eskiþehir","Afyon","Ankara","Kayseri","Gaziantep","Mersin","Samsun","Diyarbakýr","Van(Tatvan)","Van(Kapýköy)","Erzurum","Erzincan","Kars","Zonguldak","Denizli","Burdur");
var arFieldText2=new Array("Ýzmir(Alsancak)","Ýzmir(Basmane)");
var arFieldText3=new Array("Samsun");
var arFieldValue1=new Array("eskisehir","afyon","ankara","kayseri","gaziantep","mersin","samsun","diyarbakir","van1","van2","erzurum","erzincan","kars","zonguldak","denizli","burdur");
var arFieldValue2=new Array("izmir1","izmir2");
var arFieldValue3=new Array("samsun");
function change() {
for (var i=document.calcit.finish.length; i>=0; i--) {
document.calcit.finish[i] = null;
}
if(document.calcit.start.selectedIndex == 1){
//alert("1");
for (var i=0; i < arFieldValue1.length; i++)
document.calcit.finish[i] = new Option(arFieldText1[i],arFieldValue1[i]);
//alert(arFieldText1);
}
else if(document.calcit.start.selectedIndex == 2){
//alert("2");
for (var i=0; i < arFieldValue2.length; i++)
document.calcit.finish[i] = new Option(arFieldText2[i],arFieldValue2[i]);
//alert(arFieldValue2.length);
//alert(arFieldText2);
}
else if(document.calcit.start.selectedIndex == 3){
//alert("3");
for (var i=0; i < arFieldValue3.length; i++)
document.calcit.finish[i] = new Option(arFieldText3[i],arFieldValue3[i]);
}
}
function contin() {
change();
}
function makeArray() {
for (i = 0; i<makeArray.arguments.length; i++)
this[i] = makeArray.arguments[i];
}
</script>
Last edited by dethron on Thu Sep 16, 2004 8:49 pm, edited 1 time in total.
this is something i wrote before,
if you need to see it online, use http://www.servicexpress-tr.com/prices.php
if you need to see it online, use http://www.servicexpress-tr.com/prices.php
Okay, I understand now how I can use javascript to control the list displayed based on a specified value. Now how do I incorporate MySQL into the mix?
In your example dethron, you hard-coded the possible lists in javascript, but the values in the lists I am using are dynamic. The data in both lists comes from a MySQL database so I won't know until run-time what the lists are supposed to contain.
In your example dethron, you hard-coded the possible lists in javascript, but the values in the lists I am using are dynamic. The data in both lists comes from a MySQL database so I won't know until run-time what the lists are supposed to contain.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact: