How do I call up values in PHP for user input in forms (radio buttons and selects)

Posted by Derek on Stack Overflow See other posts from Stack Overflow or by Derek
Published on 2010-03-18T19:22:20Z Indexed on 2010/03/18 20:31 UTC
Read the original article Hit count: 409

Filed under:
|
|
|

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 my 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!! :)

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql