Jump to content

how to display data for updating page?


irene169

Recommended Posts

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.. :)

Link to comment
https://forums.phpfreaks.com/topic/48634-how-to-display-data-for-updating-page/
Share on other sites

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";
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.