irene169 Posted April 25, 2007 Share Posted April 25, 2007 Anyone can help? when we are displaying the data in text field inside an updating form, the code would be like this : First Name: <input type="text" name="ud_first" value="<? echo $first; ?>"> Last Name: <input type="text" name="ud_last" value="<? echo $last; ?>"> however, when it comes to the radio buttons or drop down menu, how should i write the code?? should i make it like this? --> <label> <input name="gender" type="radio" value="male" checked="<?php echo $gender;?>"/> </label> Male <label> <input name="gender" type="radio" value="female" checked="<?php echo $gender;?>"/> Female</label> and <label> <select name="prof"> <option value="<?php echo $prof; ?>" selected="selected"> <?php echo $prof?> </option> <option value="General Staff"> General Staff</option> <option value="Executive"> Executive</option> <option value="Senior Executive"> Senior Executive</option> <option value="Supervisor"> Supervisor</option> <option value="Junior Management"> Junior Management</option> <option value="Others"> Others</option> </select> </label> Your help would be highly appreciated...thanks.. Quote Link to comment Share on other sites More sharing options...
onlyican Posted April 25, 2007 Share Posted April 25, 2007 ok what I normally do. <?php //Have the array somewhere, I have a Global Array file, but this example, it is here //This works for Radio buttons, checkboxes, drop down menus $MyOptions = array(1 => "FirstValue", 2 => "Second Value"); //Lets say the Selected value from the DB is 2, as I store ID's against the name $MyOptionSelectedValue = 2; //First Radio Buttons foreach($MyOptions as $OptionValue => $OptionName){ echo "<input type='radio' name='MyRadioOptions' value='".$OptionValue."' id='".$OptionValue."'"; if($OptionValue == $MyOptionSelectedValue){ echo "checked='checked'"; } echo "/>\n" ."<label for='".$OptionValue."'>".$OptionName."'</label>\n"; } //OK, it is the same format for the Checkboxes as well, except I use array to parse the values Also, $MyOptionsSelectedValue will now be array $MyOptionSelectedValue = array(2); foreach($MyOptions as $OptionValue => $OptionName){ echo "<input type='checkbox' name='MyCheckOptions[]' value='".$OptionValue."' id='".$OptionValue."'"; if(in_array($OptionValue, $MyOptionSelectedValue){ echo "checked='checked'"; } echo "/>\n" ."<label for='".$OptionValue."'>".$OptionName."'</label>\n"; } //Final Example, DropDowns echo "<select name='MyDropDownOptions'>\n"; foreach($MyOptions as $OptionValue => $OptionName){ echo "<option value='".$OptionValue."'"; if($OptionValue == $MyOptionSelectedValue){ echo " selected='selected"; } echo "/>".$OptionName."</option>\n" } echo "</select>\n"; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.