Jump to content

Recommended Posts

Hi

 

I have spent half the day searching for some script that creates dynamic lsit/dropdown boxes. Basically depending on the selection from dropdown/list 1 will populate the list of dropdownlist 2.

Looking at the code I believe I am going to struggle updating the database, but just need confirmation on this.

 

Here is the html code

 

<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000" onload="fillCategory();">

 

<FORM name="drop_list" action="yourpage.php" method="POST" >

 

<SELECT  NAME="Category" class="qsdropdowns" onChange="SelectSubCat();" >

<Option value="">Category</option>

</SELECT>

<SELECT NAME="SubCat" class="qsdropdowns" id="SubCat">

<Option value="">SubCat</option>

</SELECT>

</form>

 

</body>

 

And here is the java code

 

function fillCategory(){

// this function is used to fill the category list on load

addOption(document.drop_list.Category, "Fruits", "Fruits", "");

addOption(document.drop_list.Category, "Games", "Games", "");

addOption(document.drop_list.Category, "Scripts", "Scripts", "");

}

 

function SelectSubCat(){

// ON selection of category this function will work

 

removeAllOptions(document.drop_list.SubCat);

addOption(document.drop_list.SubCat, "", "SubCat", "");

 

if(document.drop_list.Category.value == 'Fruits'){

addOption(document.drop_list.SubCat,"Mango", "Mango");

addOption(document.drop_list.SubCat,"Banana", "Banana");

addOption(document.drop_list.SubCat,"Orange", "Orange");

}

if(document.drop_list.Category.value == 'Games'){

addOption(document.drop_list.SubCat,"Cricket", "Cricket");

addOption(document.drop_list.SubCat,"Football", "Football");

addOption(document.drop_list.SubCat,"Polo", "Polo", "");

}

if(document.drop_list.Category.value == 'Scripts'){

addOption(document.drop_list.SubCat,"PHP", "PHP");

addOption(document.drop_list.SubCat,"ASP", "ASP");

addOption(document.drop_list.SubCat,"Perl", "Perl");

}

 

}

//////////////////

 

function removeAllOptions(selectbox)

{

var i;

for(i=selectbox.options.length-1;i>=0;i--)

{

//selectbox.options.remove(i);

selectbox.remove(i);

}

}

 

So  if I choose "Fruits" then "mangos" will the php send the values of "Fruits" and "Mangos" or would it send "Catergory" and "Sub Catergory" to the database.

 

If I am right what other options have I got to populate dropdowns from previous selections

 

Thanks in advance

Just because you are using javascript, doesn't mean that it will change the basic ways that form elements work when a form is posted.  Although you didn't include a crucial piece of the javascript code (addOption) I'm assuming it looks like this:

 

function addOption(selectbox,text,value )
{
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}

 

If so, then the value property should be getting set, and when the form is Posted, the appropriate select should have its value set.  You can easily enumerate the values of the $_POST superglob using print_r() to test this and clarify everything to your own satisfaction. 

Thanks gizmola, yes that script is in the java but just didnt copy and paste this, could I also ask would you go down the java route or would you populate the lists through php mysql I always worry about the java being turned off in the users browser.

 

Thanks

You can't get dynamic behavior without javascript or some other clientside technology.  The only other option is to require individual requests be sent each time one of the parent select boxes is chosen.  That's certainly safe and old school, but in today's dynamic/ajax/web 2.0 world, it may be highly annoying to users who may abandon the form rather than complete it.  It's really up to you and the client to decide what you want to invest.  It's possible to sniff out the availability of javascript and not use it only if the user doesn't have it available, but then of course that makes the application a lot more complicated.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.