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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.