Skip to content

Instantly share code, notes, and snippets.

@joeljerushan
Created March 30, 2018 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeljerushan/4cfd2e567e49f7174018f59586036fe2 to your computer and use it in GitHub Desktop.
Save joeljerushan/4cfd2e567e49f7174018f59586036fe2 to your computer and use it in GitHub Desktop.
Set Selected with PHP without Javascript
<select>
<?php
//init array with key value pairs
$purchase_type = array(
'1' => 'White Rice',
'2' => 'Red Rice',
'3' => 'Wheat',
'4' => 'Packets',
'5' => 'Firewood'
);
foreach($purchase_type as $key => $single) {
//echo <option value="YOUR_VALUE
echo "<option value=".$key;
//if $YOUR_RETURN_VALUE matches our key echo selected
if($YOUR_RETURN_VALUE == $key){ echo " selected"; }
//option open in line 14 close bracket here
echo '>';
//print your value
echo $single;
//close option
echo "</option>";
}
?>
</select>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment