Multiple Drop Down List Example

hobbiton73
08:22:11
Hi, I wonder whether someone may be able to help me please.

I've been working on the script shown in the example at http://www.plus2net.com/php_tutorial/dd.php which I've managed to get working and the data correctly saves to a mySQL database.

There is one issue however, that I just can't seem to resolve. My script correctly shows the text values in the first drop down menu, and then, in turn, those in the second drop down menu correctly matched to the value from the first. But, when I submit the information, instead of the text value being saved from the 'first' drop down menu, the 'id' field is saved instead. So if I were to use the demo found at the above URL as an example, if I select 'Games' and then 'Baseball', the information saved is:

Value of $cat = 3
Value of $subcat = Baseball


I was just wondering whether it was at all possible that someone could perhaps tell me please how I would get the 'text value' of the first drop down menu to save instead of the 'id' field.

So using the above as an example, the data saved would be :

Value of $cat = Games
Value of $subcat = Baseball



Many thanks and kind regards

Chris
smo1234
08-29-2011
It is usually the id is stored in the table however if you want to store the value then check the drop down list and change the option value

echo "<option value='$noticia2[cat_id]'>$noticia2[category]</option>";

change this to

echo "<option value='$noticia2[category]'>$noticia2[category]</option>";

Now you will have category in place of cat_id.
webgoonie
09-25-2011
That doesn't work on the ajax one, I believe because it's using cat_id to sort form 2 in the ajax script. how would you change this one around?
smo1234
09-25-2011
Ajax can't differentiate what is going as data. So it will pass the same data for internal processing. Now while collecting the data for subcat now you need to match category in place of cat_id.

Since you are now using string so you have to take care of special chars and space present in category while sending the data to mysql database.
ishraf
10-31-2011
Hye

I also having the same prob like webgoonie. actually i new to php and i really need help. i have tried like what you said

change this to

echo "<option value='$noticia2[category]'>$noticia2[category]</option>";

However, i still fail to make the output like
Value of $cat = Games
Value of $subcat = Baseball

So, how to take care of special chars and space present in category while sending the data to mysql database.

I really appreciated if anyone can help me with this. thanks
smo1234
11-01-2011
Here after receiving the data query also need to be changed as now a different field is to be checked for matching record.

select * from subcategory where cat_id='$cat_id'

In place of cat_id use the subcateory field name
ishraf
11-01-2011
hye smo1234

I have try to what you said but still i fail to make it run. When i change the code,the drop down fail to show any data form the row. Here i gave my code and hope so much you can look through it.I using 3 drop down list.Thank you for you help. I really appreciate it.

<div class="formDiv"><form action="/daftarsistem/inputpublic.php" method="post" name="contactform" onSubmit="return disablePage();" class="formLayer"><fieldset><legend>
Public Training(Quality &amp; Technology)</legend><br>

<label>Pick Programme / Title / Date:</label>

<?
$ebits = ini_get('error_reporting');
error_reporting($ebits ^ E_NOTICE);
///////// first list box//////////

$quer2=mysql_query("SELECT DISTINCT category,cat_id FROM category order by category");
///////////// End first list box////////////

///////second drop down list/////
$cat=$_GET['cat']; // This line is added to take care if your global variable is off

if(isset($cat) and strlen($cat) > 0){

$quer=mysql_query("SELECT DISTINCT subcategory,subcat_id FROM subcategory where subcategory=$cat order by cat_id");

}else{$quer=mysql_query("SELECT DISTINCT subcategory,subcat_id FROM subcategory order by subcategory"); }
////////// end for second drop down list box ///////////////////////////

/////// Third drop down list/////

$cat3=$_GET['cat3']; // This line is added to take care if your global variable is off
if(isset($cat3) and strlen($cat3) > 0){
$quer3=mysql_query("SELECT DISTINCT subcat2 FROM subcategory2 where subcat_id=$cat3 order by subcat2");

}else{$quer3=mysql_query("SELECT DISTINCT subcat2 FROM subcategory2 order by subcat2"); }
////////// end for third drop down list box ///////////////////////////

////////// Starting of first drop downlist /////////

echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>-- Select Programme --</option>";

while($noticia2 = mysql_fetch_array($quer2)) {

if($noticia2['cat_id']==@$cat){echo "<option selected value='$noticia2[cat_id]'>$noticia2[category]</option>"."<BR>";}

else{echo "<option value='$noticia2[cat_id]'>$noticia2[category]</option>";}
}
echo "</select>";
////////////////// This will end the first drop down list ///////////
////////// Starting of second drop downlist /////////
cho "<select name='subcat' onchange=\"reload3(this.form)\"><option value=''>-- Select Title --</option>";

while($noticia = mysql_fetch_array($quer)) {

if($noticia['subcat_id']==@$cat3){echo "<option selected value='$noticia[subcat_id]'>$noticia[subcategory]</option>"."<BR>";}

else{echo "<option value='$noticia[subcat_id]'>$noticia[subcategory]</option>";}

}
echo "</select>";

////////////////// This will end the second drop down list ///////////

////////// Starting of third drop downlist /////////

echo "<select name='subcat3' ><option value=''>-- Select Date --</option>";

while($noticia = mysql_fetch_array($quer3)) {

echo "<option value='$noticia[subcat2]'>$noticia[subcat2]</option>";

}
echo "</select>";

////////////////// This will end the third drop down list ///////////

?>
<br>
ishraf
11-04-2011
is it have to do with this javascript? please help

<SCRIPT language=JavaScript>

function reload(form)

{
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='public.php?cat=' + val ;
}
function reload3(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
var val2=form.subcat.options[form.subcat.options.selectedIndex].value;

self.location='public.php?cat=' + val + '&cat3=' + val2 ;
}

</script>
cabalsdemon
06-09-2012
$quer=mysql_query("SELECT DISTINCT subcategory,subcat_id FROM subcategory where subcategory=$cat order by cat_id");

you have to have a category data row in the subcategory table to do that query
Please Login to post your reply or start a new topic