Jump to content

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

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.