Questions Tags Users Unanswered

i want to get data from mysql database and run the program like this. cud u plz help me?

Question is open.
1 answer (2 voices) (8480 views)
Tags:
<script>
var cities = {

'state1': {'city1': 'city 1', 'city2': 'city 2'},
'state2': {'city3': 'city 3', 'city4': 'city 4'}


};
var states = {

'country1': {'state1': 'State 1', 'state2': 'State 2'}


};
function OnStateChanged (combo) {
var selstates = [];
for (var i = 0; i < combo.options.length; i++) {
if (combo.options.selected) {
selstates.push (combo.options.value);
}
}
var options = '';
for (var i = 0; i < selstates.length; i++) {
var state = selstates;
for (var city in cities[state]) {
options += '<option value="' + city + '">' + cities[state][city] + '</option>';
}
}

var citiesCombo = document.getElementById ("citiesCombo");
citiesCombo.innerHTML = options;

}
function OnCountriesChanged (combo) {
var selCountries = [];
for (var i = 0; i < combo.options.length; i++) {
if (combo.options.selected) {
selCountries.push (combo.options.value);
}
}
var options = '';
for (var i = 0; i < selCountries.length; i++) {
var country = selCountries;
for (var state in states[country]) {
options += '<option value="' + state + '">' + states[country][state] + '</option>';
}
}

var statesCombo = document.getElementById ("statesCombo");
statesCombo.innerHTML = options;
citiesCombo.innerHTML = '';

}
</script>
<select multiple="multiple" name="cbocountry[]" onchange="OnCountriesChanged(this)">
<option value="country1">Country 1</option>

</select>

<select multiple="multiple" id="statesCombo" name="cbostates[]" onchange="OnStateChanged(this)">

</select>

<select multiple="multiple" name="cbocities[]" id="citiesCombo" >

</select>
1 Answer
Hi,

Only the cities and states JavaScript objects should be created from DB.
I don't know your database structure, but you should create the $cities and $states PHP arrays first, then convert them to JSON with the json_encode function:

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
<!DOCTYPE html>
<html>
<head>
    <?php 
            /* Get data from mysql database and create the cities and states PHP arrays */
        $cities = array (
            'state1' => array ('city1' => 'city 1', 'city2' => 'city 2'),
            'state2' => array ('city3' => 'city 3', 'city4' => 'city 4'),
        );
        $states = array (
            'country1' => array ('state1' => 'State 1', 'state2' => 'State 2'),
        );
    ?>
    <script>
        var cities = <?php echo json_encode ($cities); ?>;
        var states = <?php echo json_encode ($states); ?>;
    </script>

    <script>
        function OnStateChanged (combo) {
            var selstates = [];
            for (var i = 0; i < combo.options.length; i++) {
                if (combo.options.selected) {
                    selstates.push (combo.options.value);
                }
            }
            var options = '';
            for (var i = 0; i < selstates.length; i++) {
                var state = selstates;
                for (var city in cities[state]) {
                    options += '<option value="' + city + '">' + cities[state][city] + '</option>';
                }
            }

            var citiesCombo = document.getElementById ("citiesCombo");
            citiesCombo.innerHTML = options;
        }

        function OnCountriesChanged (combo) {
            var selCountries = [];
            for (var i = 0; i < combo.options.length; i++) {
                if (combo.options.selected) {
                    selCountries.push (combo.options.value);
                }
            }
            var options = '';
            for (var i = 0; i < selCountries.length; i++) {
                var country = selCountries;
                for (var state in states[country]) {
                    options += '<option value="' + state + '">' + states[country][state] + '</option>';
                }
            }

            var statesCombo = document.getElementById ("statesCombo");
            statesCombo.innerHTML = options;
            citiesCombo.innerHTML = '';
        }
    </script>
</head>
<body>
    <select multiple="multiple" name="cbocountry[]" onchange="OnCountriesChanged(this)">
        <option value="country1">Country 1</option>
    </select>

    <select multiple="multiple" id="statesCombo" name="cbostates[]" onchange="OnStateChanged(this)">
    </select>

    <select multiple="multiple" name="cbocities[]" id="citiesCombo" >
    </select>

</body>
</html>
15 30 50 per page

Your Answer

Limit of files to attach: 3 / Max total size: 1024 KB
File:
Description:
remove