Forum Moderators: coopster

Message Too Old, No Replies

Updating records via HTML form (radio buttons, selects)

Avoiding duplicate entries and correct syntax?

         

scankified

7:11 pm on Mar 18, 2010 (gmt 0)

10+ Year Member



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

Matthew1980

7:42 pm on Mar 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there scankified,

When setting radio button's check boxes etc, you can only set the 'values' to : checked="checked" or checked="unchecked" (at least this is my understanding) as the value attribute *I don't think* doesn't apply to these input types.

This translates to:-

<input type="radio" name="booklevel" id="booklevel1" <?php echo (isset($_POST['booklevel']) && ($_POST['booklevel'] == "hard") ? 'checked="checked"':''); ?> />

This example just sets the radio button from what is sent from a 'set' $_POST['value'];

And as for this:-

<?php echo $row['bookname']?>

You need the semi-colon to action it or you will get a notice error or something.

<?php echo $row['bookname'];?>

Hope this give's you an idea of where to go from here ;-p

Cheers,
MRb

jatar_k

7:55 pm on Mar 18, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



for your select you can get rid of the echo all together and use a similar approach to the radio

you just echo " selected" if the value of the row you are outputting matches the value from the database

scankified

7:57 pm on Mar 18, 2010 (gmt 0)

10+ Year Member



This is great ,thanks MRb I reall appreciate the help!

Do you have any idea about my other problem? how to remove the duplicate entries in my select (dropdown)? The current book appears first in the drop down (as required) but then it appears again within the rest of the books? Is there a solution to this?

Greysoul

8:00 pm on Mar 18, 2010 (gmt 0)

10+ Year Member



you need to put a SELECT DISTINCT name FROM whatever in your query. this makes it choose the value only once if there are duplicates. i'm not sure if thats the correct column, but just make sure you put the DISTINCT and then the column you don't want to duplicate right after.

[w3schools.com...]

scankified

8:08 pm on Mar 18, 2010 (gmt 0)

10+ Year Member



Ok cheers, I'll try this out :)

rocknbil

3:08 am on Mar 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



checked="checked" for XHTML

checked for 4.01

A radio can hold any value.

<input type="radio" name="some-name" value="some value" checked>


$radios = Array('Easy','Medium','Difficult');
// Hard? :-)

foreach ($radios as $value) {
$radio_id = 'difficulty-' . $value;
$out .= "<input type=\"radio\" name=\"difficulty\" id=\"$radio_id\" value=\"\$value\"";
if (isset($_POST['difficulty']) and ($_POST['difficulty']==$value)) {
$out .= ' checked'; // checked=\"checked\" for XHTML
}

$out .= "> <label for=\"$radio_id\">$value</label>";
}

echo $out;

$options = Array ('A One-Ana', 'Two-Ana', 'Three-Ana');


$selectList = '
<label for="whatever">Whatever:</label>
<select name="whatever" id="whatever">
<option value="">Select</option>
';
foreach ($options as $value) {
$out .= "<option value=\"$value\"";
if (isset($_POST['whatever']) and ($_POST['whatever']==$value)) {
$out .= ' selected'; } // selected=\"selected\" for XHTML
}
$out .= ">$value</option>\n";
}
$out .= '</select>';


Or, if you use associative values, or numeric fields in databases,


$options = Array (
1 => 'A One-Ana',
2 => 'Two-Ana',
3 => 'Three-Ana'
);
//
$selectList = '
<label for="whatever">Whatever:</label>
<select name="whatever" id="whatever">
<option value="">Select</option>
';
foreach ($options as $key => $value) {
$out .= "<option value=\"$key\"";
if (isset($_POST['whatever']) and ($_POST['whatever']==$key)) {
$out .= ' selected'; } // selected=\"selected\" for XHTML
}
$out .= ">$value</option>\n";
}
$out .= '</select>';

Matthew1980

8:31 am on Mar 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes Rocknbil,
Thanks for pointing out my faux pa there. every input type has a value otherwise how would the user select the options to send the changed data! I wouldn't mind but I was doing a set of radio's the other day :/ Doh!

Cheers,
MRb

rocknbil

6:49 pm on Mar 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nah, wasn't picking on you, honest. :-) Just retentive about that stuff . . . especially when it comes to validation, which has nothing to do with PHP.

Speaking of retentive, one of the ways a radio button is supposed to work is that it always has one button checked by default. I often get requests "can we have none of them checked on load?" There are three problems with that: 1) as a programmer, both client and server side, always having one checked makes your job easier. No reason to check if isset, it will always be set (except in injection attempts,) 2) browsers, especially FF, will act very weird in refreshed pages if there isn't a default, and 3) it indeed forces the user to make selections they would/could normally ignore.

So a good handling of radios for the default value should be

if (! isset($_POST['value']) or (isset($_POST['value']) and ($_POST['value']=='this_radio_value'))) {
$radio .= ' checked';
}