View Full Version : Depending dropdownmenus with mysql database

05-12-2009, 02:03 PM
I have a problem and searched a lot on internet but haven't found the right solution for it. I found ajax but the one I found was hard coded and not from the database.

Here is my problem:

I have 2 dropdownmenu's, the second depending on the selection of the first. After the select of the second is also made an id from the database (linked to the second) is shown in a text field.

The code I already have can show the first dropdown filled but to refresh and fill the second I dont have yet (probably javascript, but dont know how).

Here is the code I have:

// open connection to MySQL server
$connection = mysql_connect('localhost', 'hgdhhgd', 'password')
or die ('Unable to connect!');

//select database
mysql_select_db('bestellingen') or die ('Unable to select database!');

$sql = 'SELECT * FROM hesk_programma ORDER BY Programma ASC';
$result = mysql_Query($sql) or die ('Error in query: $query. ' . mysql_error());

$menuContent2 = ''; $menuArray2 = '';
while ( $row = mysql_fetch_array( $result ) ) {
$menuArray2 .= " dArray['$row[Programma]'];\n";
$menuContent2 .= " <option value=\"$row[Programma_id]\">$row[Programma]</option>\n";

if( isset($_POST['Programma_id']) ){

$query = 'SELECT * FROM hesk_projecten WHERE `Project_id` = \''.mysql_real_escape_string($_POST['Programma_id']).'\'';
$result = mysql_Query($sql) or die ('Error in query: $query. ' . mysql_error());

$menuContent1 = ''; $menuArray1 = '';
while ( $row = mysql_fetch_array( $result ) ) {
$menuArray1 .= " dArray['$row[Project]'];\n";
$menuContent1 .= " <option value=\"$row[Project_id]\">$row[Project]</option>\n";

<select id="nameid2" name="name2">
<option name="select name2" value="select name2">Selecteer leverancier</option>
<?PHP echo $menuContent2; ?>

<select id="nameid1" name="name1">
<option name="select name1" value="select name1">Selecteer leverancier</option>
<?PHP echo $menuContent1; ?>

<name="projectnum" id="projectnum" size="20" value="<?php echo stripslashes(hesk_input($_SESSION['c_projectnum']));?>">

Any help would be great.

05-12-2009, 03:58 PM
Are you open to using jQuery? There is a very elegant plugin that handles this all for you, if you want to give it a shot.