Jump to content

help with menu selection based from other menu showing "selected" entry from db


mac007

Recommended Posts

Hello, All:

 

I have another problem with this second-menu-selection-based on first menu script... whcih I want to implement into tour site...

 

I picked this off the web somewhere and here it uses javascript to switch selections from db and it does work pretty well, ECXEPT I need it to remember the entered data from within the DB that is to be updated... right now, when I bring this up to update all my other text, area fields info show ok, but the menu defaults to its "Select One" option as selected as opposed to showing "Brazil" already chosen...

 

Here is the Javascript code that goes on head section, and below is the 2-menu snippet.

 

 

Appreciate any help!!

 

//// HERE FOLLOWS JAVASCRIPT/////

 

<SCRIPT language=JavaScript>

function reload(form)

{

var val=form.cat.options[form.cat.options.selectedIndex].value;

self.location='dd-modified.php?cat=' + val ;

}

 

</script>

 

 

 

//// HERE FOLLOWS 2-MENU SCRIPT/////

 

<?

 

@$cat= $_GET['cat']; // Use this line or below line if register_global is off

//@$cat=$HTTP_GET_VARS['cat']; // Use this line or above line if register_global is off

 

///////// Getting the data from Mysql table for first list box//////////

$quer2=mysql_query("SELECT DISTINCT category_name,category_id FROM item_category order by category_name");

///////////// End of query for first list box////////////

 

/////// for second drop down list we will check if category is selected else we will display all the subcategory/////

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

$quer=mysql_query("SELECT DISTINCT city_name FROM item_subcategory where country_code=$cat order by city_name");

}else{$quer=mysql_query("SELECT DISTINCT city_name FROM item_subcategory order by city_name"); }

////////// end of query for second subcategory drop down list box ///////////////////////////

 

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

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

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

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

else{echo  "<option value='$noticia2[category_id]'>$noticia2[category_name]</option>";}

}

echo "</select> <br><br>";

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

 

//////////        Starting of second drop downlist /////////

echo "<select name='subcat' id='subcat' ><option value=''>Select one</option>";

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

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

}

echo "</select>";

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

//// Add your other form fields as needed here/////

?>

 

 

 

 

 

First, your values for the option you want selected doesn't match the other options -- one has a value of 'category_id' and the other is 'category_name'.  Pick one.  But that's not your issue.

 

I don't know, but does the addition of the <BR> have anything to do with it?  Take that out, it's not needed in the <select> code -- if you want a newline, use "\n".

 

Check your HTML output and see if the 'Brazil' option (or whichever you've chosen) has the 'selected' keyword in it.  If not, your if statement is failing.  If it does, something in the HTML is conflicting with that -- is there another selected option somewhere?

yeah, I realized that about the different names, was just tryign diferent things.

 

I believe really what I am missing is the right "if-else" code to be placed within the other if's statements to show a "selected" when calling up an item to update it, as opposed to just bringing up form to insert a new which right now correctly shows "Select one" as a default...

 

thanks for you feedback!

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.