[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • 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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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