dropdown-linken-aan-een-andere-dropdown
Gesponsorde koppelingen
PHP script bestanden
-----------------------------dropdown.php-------------------------
<html>
<head>
<title>de dropdown functie></title>
<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.merk.options[form.merk.options.selectedIndex].value;
self.location='dropdown.php?merk=' + val ;
}
</script>
</head>
<body>
Code (php)
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
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
<?php
///////// pak de data uit de db//////////
$quer2=DoQuery("SELECT DISTINCT merk,merk_id FROM merk order by merk");
/////////////einde////////////
/////// als in de eerste dropdown niks geselect is dan laat de 2de drop alles zien/////
if(isset($poule) and strlen($poule) > 0){
$quer=DoQuery("SELECT DISTINCT type FROM type where merk_id=$merk order by merk_id");
}else{$quer=DoQuery("SELECT DISTINCT type FROM type order by merk_id"); }
////////// einde ///////////////////////////
echo "<form method=post name=f1 action=''>";
////////// eerste drop /////////
echo "<select name='merk' onchange=\"reload(this.form)\"><option value=''>Selecteer merk</option>";
while($line = mysql_fetch_array($quer2))
{
if($line['merk_id']==@$merk)
{echo "<option selected value='$line[merk_id]'>$line[merk]</option>"."<BR>";
}else{
echo "<option value='$line[merk_id]'>$line[merk]</option>";
}
}
echo "</select>";
////////////////// einde ///////////
////////// 2de drop /////////
echo "<select name='type'><option value=''>Selecteer type</option>";
while($line2 = mysql_fetch_array($quer)) {
echo "<option value='$line2[type_id]'>$line2[type]</option>";
}
echo "</select>";
////////////////// einde ///////////
echo "</form>";
echo "<input type='text' name='test' value=''>";
?>
///////// pak de data uit de db//////////
$quer2=DoQuery("SELECT DISTINCT merk,merk_id FROM merk order by merk");
/////////////einde////////////
/////// als in de eerste dropdown niks geselect is dan laat de 2de drop alles zien/////
if(isset($poule) and strlen($poule) > 0){
$quer=DoQuery("SELECT DISTINCT type FROM type where merk_id=$merk order by merk_id");
}else{$quer=DoQuery("SELECT DISTINCT type FROM type order by merk_id"); }
////////// einde ///////////////////////////
echo "<form method=post name=f1 action=''>";
////////// eerste drop /////////
echo "<select name='merk' onchange=\"reload(this.form)\"><option value=''>Selecteer merk</option>";
while($line = mysql_fetch_array($quer2))
{
if($line['merk_id']==@$merk)
{echo "<option selected value='$line[merk_id]'>$line[merk]</option>"."<BR>";
}else{
echo "<option value='$line[merk_id]'>$line[merk]</option>";
}
}
echo "</select>";
////////////////// einde ///////////
////////// 2de drop /////////
echo "<select name='type'><option value=''>Selecteer type</option>";
while($line2 = mysql_fetch_array($quer)) {
echo "<option value='$line2[type_id]'>$line2[type]</option>";
}
echo "</select>";
////////////////// einde ///////////
echo "</form>";
echo "<input type='text' name='test' value=''>";
?>
</body>
</html>
je moet in config.php jou mysql gegevens neerzetten.
---------------config.php-----------------
Code (php)
1
2
3
4
5
6
7
2
3
4
5
6
7
<?php
//database configuratie variabelen
$mysqlhost = 'jouw host'; //hostname of ip-nummer van de mysql server
$mysqluser = 'jou mysql inlognaam'; //username
$mysqlpws = 'jou mysql wachtwoord'; //password
$mysqldb = 'jou database'; //welke database gebruiken we binnen de mysql server
?>
//database configuratie variabelen
$mysqlhost = 'jouw host'; //hostname of ip-nummer van de mysql server
$mysqluser = 'jou mysql inlognaam'; //username
$mysqlpws = 'jou mysql wachtwoord'; //password
$mysqldb = 'jou database'; //welke database gebruiken we binnen de mysql server
?>
in db.php hoef je niks aan te doen.
------------db.php--------------
Code (php)
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
<?php
function DoQuery( $query ) //deze zielige alleenstaande functie in db.php wordt gebruikt om het verbinnden met de database in andere pagina wat te verkorten
{
global $mysqlhost, $mysqluser, $mysqlpws, $mysqldb; //gegevens uit config.php global maken anders kan ik ze hier niet gebruiken
$link = mysql_connect($mysqlhost, $mysqluser, $mysqlpws) //verbinding maken met de gegevens uit config.php
or die('Could not connect: ' . mysql_error()); //foutmelding geven bij niet kunnen connecten, dit moet misschien nog wat netter
mysql_select_db($mysqldb) or die('Could not select database'); //mysql database selecteren uit config.php
$result = mysql_query($query) or die('Query failed: ' . mysql_error()); // Perform SQL query
return $result; //resultaat terugsturen naar de vragende pagina
}
?>
function DoQuery( $query ) //deze zielige alleenstaande functie in db.php wordt gebruikt om het verbinnden met de database in andere pagina wat te verkorten
{
global $mysqlhost, $mysqluser, $mysqlpws, $mysqldb; //gegevens uit config.php global maken anders kan ik ze hier niet gebruiken
$link = mysql_connect($mysqlhost, $mysqluser, $mysqlpws) //verbinding maken met de gegevens uit config.php
or die('Could not connect: ' . mysql_error()); //foutmelding geven bij niet kunnen connecten, dit moet misschien nog wat netter
mysql_select_db($mysqldb) or die('Could not select database'); //mysql database selecteren uit config.php
$result = mysql_query($query) or die('Query failed: ' . mysql_error()); // Perform SQL query
return $result; //resultaat terugsturen naar de vragende pagina
}
?>
---------------------de database--------------
Code (php)
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
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
CREATE TABLE merk (
merk_id int(2) NOT NULL auto_increment,
merk varchar(25) NOT NULL default '',
PRIMARY KEY (merk_id)
) TYPE=MyISAM;
# # zet de gegevens in tabel `merk` #
INSERT INTO merk VALUES (1, 'Opel');
INSERT INTO merk VALUES (2, 'Ford');
INSERT INTO merk VALUES (3, 'Nissan');
INSERT INTO merk VALUES (4, 'Peugeot');
# -------------------------------------------------------- # # maak tabel `type` #
CREATE TABLE type (
type_id int(2) NOT NULL auto_increment,
merk_id int(2) NOT NULL default '0',
type varchar(25) NOT NULL default '',
PRIMARY KEY (type_id) ) TYPE=MyISAM;
# # t de gegevens in tabel `type` #
INSERT INTO type VALUES (1, 1, 'Corsa');
INSERT INTO type VALUES (2, 1, 'Vectra');
INSERT INTO type VALUES (3, 1, 'Omega');
INSERT INTO type VALUES (4, 1, 'Astra');
INSERT INTO type VALUES (5, 2, 'Focus');
INSERT INTO type VALUES (6, 2, 'Escort');
INSERT INTO type VALUES (7, 2, 'Ka');
INSERT INTO type VALUES (8, 2, 'Fiesta');
INSERT INTO type VALUES (9, 3, 'Almera');
INSERT INTO type VALUES (10, 3, 'Bluebird');
INSERT INTO type VALUES (11, 3, 'Pathfinder');
INSERT INTO type VALUES (12, 3, 'Patrol');
INSERT INTO type VALUES (13, 4, '306');
INSERT INTO type VALUES (14, 4, '405');
INSERT INTO type VALUES (15, 4, '2007');
INSERT INTO type VALUES (16, 4, '205');
merk_id int(2) NOT NULL auto_increment,
merk varchar(25) NOT NULL default '',
PRIMARY KEY (merk_id)
) TYPE=MyISAM;
# # zet de gegevens in tabel `merk` #
INSERT INTO merk VALUES (1, 'Opel');
INSERT INTO merk VALUES (2, 'Ford');
INSERT INTO merk VALUES (3, 'Nissan');
INSERT INTO merk VALUES (4, 'Peugeot');
# -------------------------------------------------------- # # maak tabel `type` #
CREATE TABLE type (
type_id int(2) NOT NULL auto_increment,
merk_id int(2) NOT NULL default '0',
type varchar(25) NOT NULL default '',
PRIMARY KEY (type_id) ) TYPE=MyISAM;
# # t de gegevens in tabel `type` #
INSERT INTO type VALUES (1, 1, 'Corsa');
INSERT INTO type VALUES (2, 1, 'Vectra');
INSERT INTO type VALUES (3, 1, 'Omega');
INSERT INTO type VALUES (4, 1, 'Astra');
INSERT INTO type VALUES (5, 2, 'Focus');
INSERT INTO type VALUES (6, 2, 'Escort');
INSERT INTO type VALUES (7, 2, 'Ka');
INSERT INTO type VALUES (8, 2, 'Fiesta');
INSERT INTO type VALUES (9, 3, 'Almera');
INSERT INTO type VALUES (10, 3, 'Bluebird');
INSERT INTO type VALUES (11, 3, 'Pathfinder');
INSERT INTO type VALUES (12, 3, 'Patrol');
INSERT INTO type VALUES (13, 4, '306');
INSERT INTO type VALUES (14, 4, '405');
INSERT INTO type VALUES (15, 4, '2007');
INSERT INTO type VALUES (16, 4, '205');