• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

Syntax Correction for Drop Down List Selection

I have generated a drop down list with php but I can't seem to set the selected value. Could someone please correct my mistake.
I have attached the code.

Thanks
$dataset = array("January","February","March","April","May","June","July","August","September","October","November","December");
 
$test = "January";
echo "<select name=vendorddl>";
echo "<option value="."---".">"."---"."</option>";
$numberofrecords = count($dataset);	
	for ($x=0; $x<$numberofrecords; $x++)
	{
		$selected = $test;
	    $lbvalue = $dataset[$x];
		echo "<option value=".$lbvalue."selected="."'".$selected."'".">".$lbvalue."</option>";
		
	}
echo "</select>";// Closing of list box

Open in new window

0
robinrl2008
Asked:
robinrl2008
1 Solution
 
MMDeveloperCommented:
you are writing selected='January' which is invalid HTML... try this


$dataset = array("January","February","March","April","May","June","July","August","September","October","November","December");
 
$test = "January";
echo "<select name=vendorddl>";
echo "<option value="."---".">"."---"."</option>";
$numberofrecords = count($dataset);     
        for ($x=0; $x<$numberofrecords; $x++)
        {
		$lbvalue = $dataset[$x];
		if ($lbvalue == $test) {
			$selected = ' selected="selected"';
		}
		else {
			$selected = "";
		}
		echo '<option value="' . $lbvalue . '" ' . $selected . '>' . $lbvalue . '</option>';
        }
echo "</select>";// Closing of list box

Open in new window

0
 
robinrl2008Author Commented:
Thanks, I really love this site it makes my work much easier and less time consuming. You Rock!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now