Jump to content

Populating a Drop Down Menu


zachatk1

Recommended Posts

Hey, I don't know javascript and I have this code that is a dynamic drop down list.

 

The 2 PHP variables are these:

 

$sel1 = $_POST["sel1"]
$sel2 = $_POST["sel2"]

 

Here's the javascript, I took out the huge amounts of selections:

 

First_selection = new Array(
"1",
"2",
"3",
.... Lots of data
);

First selection second array = new Array(
...more data
);


function changeval()
{
var val1 = document.change.sel1.value;
var optionArray = eval(val1);
for(var df=0; df<optionArray.length; df++)
{
  var ss = document.change.sel2;
  ss.options.length = 0;
  for(var ff=0; ff<optionArray.length; ff++)
  {
   var val = optionArray[ff];
   ss.options[ff] = new Option(val,val);
  }
}
}
  </script>

<form name="change" action="verify.php" method="post">

<select name=sel1 onchange=changeval()>
<script type="text/javascript">
for(var model=0; model<make.length; model++)
{
  document.write("<option value=\""+make[model]+"\">"+make[model]+"</option>");
}
</script>
</select>
<select name=sel2>
</select>

 

When the page loads, the drop down menu needs to have sel1 and sel2 selected in the drop down list. Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/237140-populating-a-drop-down-menu/
Share on other sites

In the for loop do an if:

 

if (make[model] == <?php echo $sel1 ?>)
   document.write("<option value=\""+make[model]+"\" selected=\"selected\">"+make[model]+"</option>");
else
   document.write("<option value=\""+make[model]+"\">"+make[model]+"</option>");

Your going to have to check the \" s  but you get the idea

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.