dynamic-linked-dropdown-menu-phpmysqljavascript

Gesponsorde koppelingen

PHP script bestanden

  1. dynamic-linked-dropdown-menu-phpmysqljavascript

« Lees de omschrijving en reacties

CODE VOOR DUAL-SELECT

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?
// Database connection
$db_database = 'databasename';
$db_host = 'localhost';
$db_user = 'username';
$db_pass = 'password';
mysql_connect($db_host,$db_user,$db_pass) or die("Could not connect to MySQL (Main connection)");
mysql_select_db($db_database) or die("Could not connect to database (Main connection)");


function
dual_linked_select(
$table,            // Table to create linked selects from
$table_column_01,        // Root category
$table_column_02,        // Subcategory of the root category
$group_02_default_option_text    // Default option text for the group 2 select
)
{


// Define globals
    global $javascript;
    global $group_01_options;
    // Define variables
    $javascript = null; // Hold
    $group_01_options = null;//Hold

// Assembly of Javascript starts

    $javascript .=<<<content
    /* Linked Dropdown Selects Script Start */
    function DefaultGroup2()
    {
        var x = document.getElementById("Group2");
        x.length = 0;
        VarGroup2 = document.getElementById("Group2");
        VarGroup2.options[VarGroup2.options.length] = new Option("$group_02_default_option_text","");
        document.getElementById("Group2").disabled = true;
    }

    function CheckGroup1Select()
    {
        // If no Group1 is selected clear the Group2 and set to default
        if(document.getElementById("Group1").value == "")
        {
        // Clear the Group2 select and set to default value
        DefaultGroup2();
        }

content
;

// Javascript
    $group_01_query = "SELECT ".$table_column_01." FROM ".$table." GROUP BY ".$table_column_01;
    $group_01_result = mysql_query($group_01_query) or die(mysql_error());
    while($group_01 = mysql_fetch_array($group_01_result))
    {

        $group_01_options .= '<option value="'.$group_01[$table_column_01].'">'.$group_01[$table_column_01].'</option>'."\r\n";

        $javascript .=<<<content
        else if(document.getElementById("Group1").value == "$group_01[$table_column_01]")
        {
            // Clear the Group2 and set to default value
            DefaultGroup2();
            // Set variable options for Group2 select
            VarGroup2 = document.getElementById("Group2");

content
;

        $group_02_query = "SELECT ".$table_column_02." FROM ".$table." WHERE ".$table_column_01." = '".$group_01[$table_column_01]."' GROUP BY ".$table_column_02;
        $group_02_result = mysql_query($group_02_query) or die(mysql_error());
        while($group_02 = mysql_fetch_array($group_02_result))
        {

            $javascript .='            VarGroup2.options[VarGroup2.options.length] = new Option("'.$group_02[$table_column_02].'","'.$group_02[$table_column_02].'");'."\r\n";
        }


            $javascript .=<<<content
            document.getElementById("Group2").disabled = false;

        }

content
;
    }

        $javascript .='    }'."\r\n";

}
// End of dual_linked_selects function
?>


<?
// execute dual_linked_selects_function
dual_linked_select('tabel_name','table_column1','tablecolumn2','Select...');
?>


<html>
<head>
    <title>Dual Linked Selects with PHP and MYSQL (without page reload)</title>
<script type="text/javascript" language="JavaScript">
<? echo $javascript; ?>
</script>
</head>

<body>
<form action="">
Group1:
<br>
<select id="Group1" name="group1" onchange="CheckGroup1Select(this);">
<option value="">Select...</option>
<? echo $group_01_options; ?>
</select>

<br>
Group2:
<br>
<select id="Group2" name="group2"></select>

</form>
</body>

</html>


CODE VOOR TRIPLE SELECT

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?
// Database connection
$db_database = 'databasename';
$db_host = 'localhost';
$db_user = 'username';
$db_pass = 'password';
mysql_connect($db_host,$db_user,$db_pass) or die("Could not connect to MySQL (Main connection)");
mysql_select_db($db_database) or die("Could not connect to database (Main connection)");

// Start of triple_linked_selects function
function triple_linked_selects(
$table,        // Table to create linked selects from
$table_column_01,    // Root category
$table_column_02,    // Subcategory of the root category
$table_column_03,    // Available items in subcategory of the root category
$group_02_default_option_text,    // Default option text for the group 2 select
$group_03_default_option_text)    // Default option text for the group 3 select

{
// Define globals
    global $javascript;
    global $group_01_options;
// Define variables
    $javascript = null; // Holds all the generated javascript
    $group_01_options = null; // Holds all the select options for group 1

// Static Javascript

    $javascript .=<<<content
    /* Linked Dropdown Selects Script Start */
    function DefaultGroup2()
    {
        var x = document.getElementById("Group2");
        x.length = 0;
        VarGroup2 = document.getElementById("Group2");
        VarGroup2.options[VarGroup2.options.length] = new Option("$group_02_default_option_text","");
        document.getElementById("Group2").disabled = true;
    }

    function DefaultGroup3()
    {
        var x = document.getElementById("Group3");
        x.length = 0;
        VarGroup3 = document.getElementById("Group3");
        VarGroup3.options[VarGroup3.options.length] = new Option("$group_03_default_option_text","");
        document.getElementById("Group3").disabled = true;
    }

    function ClearGroup3()
    {
        var x = document.getElementById("Group3");
        x.length = 0;
        document.getElementById("Group3").disabled = false;
    }

    function CheckGroup1Select()
    {
        // If no Group1 is selected clear the Group2 and Group3 selects and set them to their default values
        if(document.getElementById("Group1").value == "")
        {
            // Clear the Group2 select and set to default value
            DefaultGroup2();
            // Clear the Group3 select and set to default value
            DefaultGroup3();
        }

content
;

// Groups 1 & 2 Javascript
    $group_01_query = "SELECT ".$table_column_01." FROM ".$table." GROUP BY ".$table_column_01;
    $group_01_result = mysql_query($group_01_query) or die(mysql_error());
    while($group_01 = mysql_fetch_array($group_01_result))
    {

        $group_01_options .= '<option value="'.$group_01[$table_column_01].'">'.$group_01[$table_column_01].'</option>'."\r\n";

        $javascript .=<<<content
        else if(document.getElementById("Group1").value == "$group_01[$table_column_01]")
        {
            // Clear the Group2 select and set to default value
            DefaultGroup2();
            // Set dynamic options for Group2 select
            VarGroup2 = document.getElementById("Group2");

content
;

        $group_02_query = "SELECT ".$table_column_02." FROM ".$table." WHERE ".$table_column_01." = '".$group_01[$table_column_01]."' GROUP BY ".$table_column_02;
        $group_02_result = mysql_query($group_02_query) or die(mysql_error());
        while($group_02 = mysql_fetch_array($group_02_result))
        {

            $javascript .='            VarGroup2.options[VarGroup2.options.length] = new Option("'.$group_02[$table_column_02].'","'.$group_02[$table_column_02].'");'."\r\n";
        }


            $javascript .=<<<content
            document.getElementById("Group2").disabled = false;
            // Clear the Group3 select and set to default value
            DefaultGroup3();
        }

content
;
    }

        $javascript .='    }'."\r\n";

// Group 3 Javascript

    $javascript .=<<<content
    function CheckGroup2Select()
    {

content
;

    $group_01_query = "SELECT ".$table_column_01." FROM ".$table." GROUP BY ".$table_column_01;
    $group_01_result = mysql_query($group_01_query) or die(mysql_error());
    while($group_01 = mysql_fetch_array($group_01_result))
    {


    $javascript .=<<<content
        if((document.getElementById("Group1").value == "$group_01[$table_column_01]") && (!document.getElementById("Group2").value))
        {
            // Clear the Group3 select and set to default value
            DefaultGroup3();
        }

content
;

        $group_02_query = "SELECT ".$table_column_02." FROM ".$table." WHERE ".$table_column_01." = '".$group_01[$table_column_01]."' GROUP BY ".$table_column_01.",".$table_column_02;
        $group_02_result = mysql_query($group_02_query) or die(mysql_error());
        while($group_02 = mysql_fetch_array($group_02_result))
        {


        $javascript .=<<<content
        else if((document.getElementById("Group1").value == "$group_01[$table_column_01]") && (document.getElementById("Group2").value == "$group_02[$table_column_02]"))
        {
            ClearGroup3();
            VarGroup3 = document.getElementById("Group3");

content
;

            $group_03_query = "SELECT * FROM ".$table." WHERE ".$table_column_02." = '".$group_02[$table_column_02]."' AND ".$table_column_01." = '".$group_01[$table_column_01]."'  GROUP BY ".$table_column_03;
            $group_03_result = mysql_query($group_03_query) or die(mysql_error());
            while($group_03 = mysql_fetch_array($group_03_result))
            {

                $javascript .='            VarGroup3.options[VarGroup3.options.length] = new Option("'.$group_03[$table_column_03].'","'.$group_03[$table_column_03].'");'."\r\n";
            }

    $javascript .='        }'."\r\n";
        }
    }


    $javascript .=<<<content
    }

    window.onload = CheckGroup1Select;
    /* Linked Dropdown Selects Script End */

content
;

}
// End of triple_linked_selects function
?>


<?
// Call the triple_linked_selects function
triple_linked_selects('tabel_name','table_column1','tablecolumn2','tablecolumn3','Select...','------------');
?>


<html>
<head>
    <title>Triple Linked Selects</title>
<script type="text/javascript" language="JavaScript">
<? echo $javascript; ?>
</script>
</head>

<body>
<form action="">
Group1:
<br>
<select id="Group1" name="Group1" onchange="CheckGroup1Select(this);">
<option value="">Select...</option>
<? echo $group_01_options; ?>
</select>

<br>
Group2:
<br>
<select id="Group2" name="Group2" onchange="CheckGroup2Select(this);"></select>

<br>
Group3:
<br>
<select id="Group3" name="Group3"></select>
</form>
</body>
</html>


CODE VOOR QUAD SELECT

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?
// Database connection
$db_database = 'databasename';
$db_host = 'localhost';
$db_user = 'username';
$db_pass = 'password';
mysql_connect($db_host,$db_user,$db_pass) or die("Could not connect to MySQL (Main connection)");
mysql_select_db($db_database) or die("Could not connect to database (Main connection)");

// Start of quad_linked_selects function
function quad_linked_selects(
$table,        // Table name
$table_column_01,    // Root category
$table_column_02,    // Available items in sub category
$table_column_03,    // Available items in sub-sub category
$table_column_04,    // Available items in sub-sub-sub category
$group_02_default_option_text,    // Default option text for the group 2 select
$group_03_default_option_text,    // Default option text for the group 3 select
$group_04_default_option_text)    // Default option text for the group 4 select
{
// Define globals
    global $javascript;
    global $group_01_options;
// Define variables
    $javascript = null; // Holds all the generated javascript
    $group_01_options = null; // Holds all the select options for group 1

// Static Javascript

    $javascript .=<<<content
    /* Linked Dropdown Selects Script Start */
    function DefaultGroup2()
    {
        var x = document.getElementById("Group2");
        x.length = 0;
        VarGroup2 = document.getElementById("Group2");
        VarGroup2.options[VarGroup2.options.length] = new Option("$group_02_default_option_text","");
        document.getElementById("Group2").disabled = true;
    }

    function DefaultGroup3()
    {
        var x = document.getElementById("Group3");
        x.length = 0;
        VarGroup3 = document.getElementById("Group3");
        VarGroup3.options[VarGroup3.options.length] = new Option("$group_03_default_option_text","");
        document.getElementById("Group3").disabled = true;
    }

    function DefaultGroup4()
    {
        var x = document.getElementById("Group4");
        x.length = 0;
        VarGroup4 = document.getElementById("Group4");
        VarGroup4.options[VarGroup4.options.length] = new Option("$group_04_default_option_text","");
        document.getElementById("Group4").disabled = true;
    }

    function ClearGroup3()
    {
        var x = document.getElementById("Group3");
        x.length = 0;
        document.getElementById("Group3").disabled = false;
    }

    function ClearGroup4()
    {
        var x = document.getElementById("Group4");
        x.length = 0;
        document.getElementById("Group4").disabled = false;
    }

    function CheckGroup1Select()
    {
        // If no Group1 is selected clear the Group2 and Group3 selects and set them to their default values
        if(document.getElementById("Group1").value == "")
        {
            // Clear the Group2 select and set to default value
            DefaultGroup2();
            // Clear the Group3 select and set to default value
            DefaultGroup3();
            // Clear the Group4 select and set to default value
            DefaultGroup4();
        }

content
;

// Groups 1 & 2 Javascript
    $group_01_query = "SELECT ".$table_column_01." FROM ".$table." GROUP BY ".$table_column_01;
    $group_01_result = mysql_query($group_01_query) or die(mysql_error());
    while($group_01 = mysql_fetch_array($group_01_result))
    {

        $group_01_options .= '<option value="'.$group_01[$table_column_01].'">'.$group_01[$table_column_01].'</option>'."\r\n";

        $javascript .=<<<content
        else if(document.getElementById("Group1").value == "$group_01[$table_column_01]")
        {
            // Clear the Group2 select and set to default value
            DefaultGroup2();
            // Set dynamic options for Group2 select
            VarGroup2 = document.getElementById("Group2");

content
;

        $group_02_query = "SELECT ".$table_column_02." FROM ".$table." WHERE ".$table_column_01." = '".$group_01[$table_column_01]."' GROUP BY ".$table_column_02;
        $group_02_result = mysql_query($group_02_query) or die(mysql_error());
        while($group_02 = mysql_fetch_array($group_02_result))
        {

            $javascript .='            VarGroup2.options[VarGroup2.options.length] = new Option("'.$group_02[$table_column_02].'","'.$group_02[$table_column_02].'");'."\r\n";
        }


            $javascript .=<<<content
            document.getElementById("Group2").disabled = false;
            // Clear the Group3 select and set to default value
            DefaultGroup3();
            DefaultGroup4();
        }

content
;
    }

        $javascript .='    }'."\r\n";

// Group 3 Javascript
    $javascript .=<<<content
    function CheckGroup2Select()
    {

content
;

    $group_01_query = "SELECT ".$table_column_01." FROM ".$table." GROUP BY ".$table_column_01;
    $group_01_result = mysql_query($group_01_query) or die(mysql_error());
    while($group_01 = mysql_fetch_array($group_01_result))
    {


    $javascript .=<<<content
        if((document.getElementById("Group1").value == "$group_01[$table_column_01]") && (!document.getElementById("Group2").value))
        {
            // Clear the Group3&4 select and set to default value
            DefaultGroup3();
            DefaultGroup4();
        }

content
;

        $group_02_query = "SELECT ".$table_column_02." FROM ".$table." WHERE ".$table_column_01." = '".$group_01[$table_column_01]."' GROUP BY ".$table_column_01.",".$table_column_02;
        $group_02_result = mysql_query($group_02_query) or die(mysql_error());
        while($group_02 = mysql_fetch_array($group_02_result))
        {


        $javascript .=<<<content
        else if((document.getElementById("Group1").value == "$group_01[$table_column_01]") && (document.getElementById("Group2").value == "$group_02[$table_column_02]"))
        {
            DefaultGroup3();
            VarGroup3 = document.getElementById("Group3");

content
;
            $group_03_query = "SELECT * FROM ".$table." WHERE ".$table_column_02." = '".$group_02[$table_column_02]."' AND ".$table_column_01." = '".$group_01[$table_column_01]."'  GROUP BY ".$table_column_03;
            $group_03_result = mysql_query($group_03_query) or die(mysql_error());
            while($group_03 = mysql_fetch_array($group_03_result))
            {

                $javascript .='            VarGroup3.options[VarGroup3.options.length] = new Option("'.$group_03[$table_column_03].'","'.$group_03[$table_column_03].'");'."\r\n"            ;
            }

    $javascript .='            document.getElementById("Group3").disabled = false;
            DefaultGroup4();
        }'
."\r\n";
        }
    }


    $javascript .=<<<content
    }
    

content
;


// Group 4 Javascript
    $javascript .=<<<content
    function CheckGroup3Select()
    {

content
;

    $group_01_query = "SELECT ".$table_column_01.", ".$table_column_02." FROM ".$table." GROUP BY ".$table_column_02;
    $group_01_result = mysql_query($group_01_query) or die(mysql_error());
    while($group_01 = mysql_fetch_array($group_01_result))
    {


    $javascript .=<<<content
        if((document.getElementById("Group2").value == "$group_01[$table_column_02]") && (document.getElementById("Group3").value == "$group_01[$table_column_03]") && (!document.getElementById("Group4").value))
        {
            // Clear the Group4 select and set to default value
            DefaultGroup4();
        }

content
;

        $group_02_query = "SELECT ".$table_column_02.", ".$table_column_03." FROM ".$table." WHERE ".$table_column_02." = '".$group_01[$table_column_02]."' GROUP BY ".$table_column_02.",".$table_column_03;
        $group_02_result = mysql_query($group_02_query) or die(mysql_error());
        while($group_02 = mysql_fetch_array($group_02_result))
        {


        $javascript .=<<<content
        else if((document.getElementById("Group2").value == "$group_01[$table_column_02]") && (document.getElementById("Group3").value == "$group_02[$table_column_03]"))
        {
            DefaultGroup4();
            VarGroup4 = document.getElementById("Group4");

content
;

            $group_03_query = "SELECT * FROM ".$table." WHERE ".$table_column_03." = '".$group_02[$table_column_03]."' AND ".$table_column_02." = '".$group_01[$table_column_02]."'  GROUP BY ".$table_column_04;
            $group_03_result = mysql_query($group_03_query) or die(mysql_error());
            while($group_03 = mysql_fetch_array($group_03_result))
            {

                $javascript .='            VarGroup4.options[VarGroup4.options.length] = new Option("'.$group_03[$table_column_04].'","'.$group_03[$table_column_04].'");'."\r\n";
            }

    $javascript .='            document.getElementById("Group4").disabled = false;

                        }'
."\r\n";
        }
    }


    $javascript .=<<<content
    }

    window.onload = CheckGroup1Select;
    /* Linked Dropdown Selects Script End */

content
;



}
// End of quad_linked_selects function
?>


<?
// Call the quad_linked_selects function
quad_linked_selects('tablename','tablecolumn1','tablecolumn2','tablecolumn3','tablecolumn4','Select...','------------','------------');
?>



<html>
<head>
    <title>Triple Linked Selects</title>
<script type="text/javascript" language="JavaScript">
<? echo $javascript; ?>
</script>
</head>

<body>
<form action="">
Group1:
<br>
<select id="Group1" name="Group1" onchange="CheckGroup1Select(this);">
<option value="">Select...</option>
<? echo $group_01_options; ?>
</select>

<br>
Group2:
<br>
<select id="Group2" name="Group2" onchange="CheckGroup2Select(this);"></select>

<br>
Group3:
<br>
<select id="Group3" name="Group3" onchange="CheckGroup3Select(this);"></select>

<br>
Group4:
<br>
<select id="Group4" name="Group4"></select>

</form>
</body>
</html>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.