How do I call up values in PHP for user input in forms (radio buttons and selects)
Ok so my admin sets to edit a book which was created. I know how to bring in the values that were initially entered via a simple text field like 'bookname'. On the edit book page the book name field stores the currently assigned 'bookname' in the field (which is what I want! :) )
However I have other field types like selects and radio button entries...I'm having trouble calling in the already set value when the book was created.
For example, there is a 'booklevel' field, which I have set as radio button entries as; Hard, Normal, and Easy. When the user goes to edit the book, I'm not too sure on how to have the current value drawn up (its stored as text) and the radio button being checked. I.e. 'Normal' is checked if this is what was set when the book was created. So far I have this as the code for the adding book level:
<label>Book Level:</label> <label for="booklevel1" class="radio">Hard
<input type="radio" name="booklevel" id="booklevel1"
value="<?php echo 'Hard'; if (isset($_POST['booklevel'])); ?>"></label>
<label for="booklevel2" class="radio">Medium<input type="radio" name="booklevel"
id="booklevel2"
value="<?php echo 'Normal'; if (isset($_POST['booklevel'])); ?>"></label>
<label for="booklevel" class="radio">Low<input type="radio" name="booklevel"
id="booklevel3"
value="<?php echo 'Easy'; if (isset($_POST['booklevel'])); ?>"></label>
This all works fine by the way when the user adds the book... But does anyone know how in my update book form, I can draw the value of what level has been set, and have the box checked?? To draw up the values in the text fields, I'm simply using:
<?php echo $row['bookname']?>
I also noticed a small issue when I call up the values for m开发者_C百科y Select options. I have the drop down select field display the currently set user (to read the book!), however, the drop down menu again displays the user in the list available options to select - basically meaning 2 of the same names appear in the list! Is there a way to eliminate the value of the SELECTED option? So far my setup for this is like:
<select name="user_id" id="user_id">
<option value="<?php echo $row['user_id']?>" SELECTED><?php echo $row['fullname']?></option>
<?php
while($row = mysql_fetch_array($result))
{ ?> <option value="<?php echo $row['user_id']?>"><?php echo $row['name']?></option>
<?php } ?>
</select>
If anyone can help me I'll be very greatful. Sorry for the incredibly long question!! :)
For the radio buttons, simply add the attribute checked="true"
if the current button's value matches the value you already have. For the dropdown, don't use the first option
you have there, but instead use the same technique as the radio buttons, but with selected="true"
.
Check to see which string is stored in the DB (easy, normal, hard) and then set the checked
field to 'checked'. For example:
<label>Book Level:</label> <label for="booklevel1" class="radio">Hard
<input type="radio" name="booklevel" id="booklevel1"
value="hard" <?php if($row['booklevel'] == 'hard') echo 'checked="checked"'; ?>></label>
As to the second part - you'll need to store the first value of $row['user_id']
in a temporary variable before entering the while loop. Then check each additional user_id you receive to determine whether it is equal. If it is, don't print its option. Like so:
<?php
$tmp = $row['user_id'];
while($row = mysql_fetch_array($result)) {
if($row['user_id'] != $tmp) {
echo '<option value="'.$row['user_id'].'">'.$row['name'].'</option>';
}
}
?>
精彩评论