Created
March 30, 2018 12:56
-
-
Save joeljerushan/4cfd2e567e49f7174018f59586036fe2 to your computer and use it in GitHub Desktop.
Set Selected with PHP without Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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