I have a script that contains 3 dropdown list. 1 country 2 region 3 city
I wonder how i can populate the script from database
because if i leave it like this works fine but i want to extract the country from database mysql. if somebody knows how can i do that please help.
This would work if you had a table for each country name. I'm not sure if I'm understanding your question right. Are you trying to use Ajax methods to get the dropdown to update? Even with Ajax you will still need PHP to access the DB.
let me ask again. I have this dropdown lists that are populate from an external file called countryCityList.php where i have array from each country and i want to populate this list from that array only that I want this array to have the data from database. Tha data are from a table called members where i have country region and city.
This are the lists:
CODE:
And this the external file where i want to have the arrays from the database if this could happen:
CODE:
var states = new Array();
states['Germany'] = new Array('Bayern','Berlin','Hamburg');
states['Morocco'] = new Array('Casablanca','Doukkala Abda','Guelmin');
states['United Kingdom'] = new Array('England','Scotland','Wales','Northern Ireland');
// City lists
var cities = new Array();
cities['Germany'] = new Array();
cities['Germany']['Bayern'] = new Array('Ainring');
cities['Germany']['Berlin'] = new Array('Berlin');
cities['Germany']['Hamburg'] = new Array('Hamburg');
cities['Morocco'] = new Array();
cities['Morocco']['Casablanca'] = new Array('ad-Dar-al-Baida');
cities['Morocco']['Doukkala Abda'] = new Array('al-Jadidah');
cities['Morocco']['Guelmin'] = new Array('Gulimim');
cities['United Kingdom'] = new Array();
cities['United Kingdom']['England'] = new Array('Abingdon');
cities['United Kingdom']['Scotland'] = new Array('Alford');
cities['United Kingdom']['Wales'] = new Array('Maesteg');
cities['United Kingdom']['Northern Ireland'] = new Array('Coleraine');
Well, PHP and Javascript don't go together. As far as I know you can't access a database with javascript, it's possible I'm wrong on this though. From my knowledge you would not be able to accomplish what you are asking because you need to have the PHP access the database. If someone else has a way to do this please reply on this thread so I may learn as well.
Ok. Thanks for this reply. Well if i am connecting to database and extract data like this:
Code:
<?php
$sql="SELECT country FROM members";
$result =mysql_query($sql);
while ($country=mysql_fetch_assoc($result)){
?>
<option value ="<?php echo $country['id'] ?>" >
<?php echo $country['country'] ?>
</option>
<?php } ?>
how can i put that data to this array
Code:
var states = new Array();
states['Germany'] = new Array('Bayern','Berlin','Hamburg');
states['Morocco'] = new Array('Casablanca','Doukkala Abda','Guelmin');
states['United Kingdom'] = new Array('England','Scotland','Wales','Northern Ireland');
Of course I want to get rid of this long script and get this data from above from mysql database.
Please let me know if you understand what i say and please help me if you can. I really need to now this.
Help! Please! I would really apreciate any help. I think I am not the only one who wants to find if something like this work.
What about ajax? I know that ajax can connect to a database but how can i extract that data into states array or the other one.
In my database are all the country region and cities and the script I put it here all. All I want is that these array from .js file to be gone and to have arrays that extract data from database. database is selectstates, table is members and then are country,region and city... My first try was to pass php array from query to this new java script array but it didn't work and I delete everything.
Look for an example here:
postcrossing(dot)com(slash)signup
That form extracts all from database in that lists.
That is what I want but I need to have this javascript function to change the region when the first choice is made and then the to change the third when the second chance is made.
yes javascript and php can be used together. php to do the database(SERVER) side commands and javascript to the page(CLIENT-SIDE ) commands. Its getting the two to work together that you need to do.
I'm trying to do the same thing... I have 5 sites that need the same thing. When i get it working i will post...
Bookmarks