Page 1 of 1
Radio Buttons - can't get pre-poulated form right
Posted: Sat Mar 18, 2006 3:38 am
by Noobie
Hi
I've pretty much got everything working as I want EXCEPT one tiny thing which I KNOW is simple but can't for the life of me figure out - been back and forwards with small variations so many times that I'm getting a bit lost.
What I have is a pre-populated form used to update the entry in the db - it includes two radio buttons with the values of "1" and "0". These radio buttons work - by which I mean that if you click either of them then the result is changed in the db. The problem is that I want the current state (either 0 or 1) to be in the form when it's shown to the user as per the other fields.
The code that's not working currently is:
Code: Select all
<input type="radio" id="showjob" name="showjob" value="0" <? if (isset($POST['0'])) { echo"checked"; } ?> />No
<input type="radio" id="showjob" name="showjob" value="1" <? if (isset($POST['1'])) { echo"checked"; } ?> />Yes
I know I need to check each button to see if the db value is equal to the value in the html and if it is then echo "checked" but I can't get the syntax right.
Thanks in advance.
Posted: Sat Mar 18, 2006 4:07 am
by matthijs
Code: Select all
<input type="radio" id="showjob" name="showjob" value="0"
<?php
if (isset($_POST['showjob']) && $_POST['showjob'] == '0' ) { echo 'checked="checked"'; }
?>/>No
<input type="radio" id="showjob" name="showjob" value="1"
<?php
if (isset($_POST['showjob']) && $_POST['showjob'] == '1' ) { echo 'checked="checked"'; }
?>/>Yes
Posted: Sat Mar 18, 2006 4:24 am
by Noobie
Thanks for replying matthijs - I added your code but it still doesn't seem to work.
Here's the full code (minus the db call bit) maybe I've missed something else?
Code: Select all
// retrieve the row from the database
$query = "SELECT * FROM jobs WHERE id='$id' ";
$result = mysql_query( $query );
// print out the results
if( $result && $jobs = mysql_fetch_object( $result ) )
{
// print out the info
$jobid = $jobs -> jobid;
$jobtitle = $jobs -> jobtitle;
$jobdescrip = $jobs -> jobdescrip;
$showjob = $jobs -> showjob;
// special characters
$jobid = htmlspecialchars($jobid);
$jobtitle = htmlspecialchars($jobtitle);
$jobdescrip = htmlspecialchars($jobdescrip);
}}
?>
<form action="savejob.php" method="get">
<fieldset style="border:none;">
<input value="<?php echo ($id) ?>" type="hidden" name="id" />
<table summary="editform">
<tr><td><input type="radio" id="showjob" name="showjob" value="0" <?php if (isset($_POST['showjob']) && $_POST['showjob'] == '0' ) { echo 'checked="checked"'; } ?>/>No
<input type="radio" id="showjob" name="showjob" value="1" <?php if (isset($_POST['showjob']) && $_POST['showjob'] == '1' ) { echo 'checked="checked"'; } ?>/>Yes <tr><td>
<label for="jobtitle">Job Title:</label></td>
<td><input type="text" value="<?php echo ($jobtitle) ?>" name="jobtitle" title="input job title" id="jobtitle" size="40" /></td></tr>
<tr><td><label for="jobdescrip">Job Description:</label></td>
<td><textarea name="jobdescrip" id="jobdescrip" rows="15" cols="50"><?php echo ($jobdescrip) ?></textarea></td></tr>
<tr><td></td><td>
<input type="submit" value="Submit Vacancy Details" /></td></tr>
</table>
</fieldset>
</form>
Everything else seems to work as it should - and if you click the radio buttons the results are stored - they're just not pre-populated.
Thanks
Posted: Sat Mar 18, 2006 6:59 am
by jayshields
Why not try selected="selected" instead of checked="checked"?
I can't say I've ever pre-populated radio buttons, but for drop downs it's selected, and for checkboxes it's checked, so maybe it's selected for radio buttons?
Posted: Sat Mar 18, 2006 7:19 am
by Noobie
Hi Jay - I gave it a go but still no good. The radio buttons still work - but no checked/selected automatically.
Posted: Sat Mar 18, 2006 7:44 am
by jayshields
Well, your code looks OK to me.
Why not change the values in HTML: 1 => yes, 0 => no. Maybe it doesn't like the fact you're using 0 and 1. Obviously then change your if statement in your PHP.
Posted: Sat Mar 18, 2006 8:06 am
by Noobie
I think I'll give up for now as I've got so many other pages relying on the 0 and 1 and I can't face going in and changing 'em all!!
I really appreciate your suggestions though - thanks for your help.

Posted: Sat Mar 18, 2006 11:47 am
by Benjamin
Radio buttons use the checked attribute. selected won't populate them.
Might want to give this a go:
Code: Select all
if (isset($_POST['showjob']) && $_POST['showjob'] === '0' ) {
echo ' checked ';
}
?>
Posted: Sat Mar 18, 2006 1:09 pm
by feyd
of note, XHTML, like the original poster appears to be using will require