Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.
Moderator: General Moderators
Noobie
Forum Commoner
Posts: 85 Joined: Sun May 15, 2005 11:38 am
Post
by Noobie » Sat Mar 18, 2006 3:38 am
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.
matthijs
DevNet Master
Posts: 3360 Joined: Thu Oct 06, 2005 3:57 pm
Post
by matthijs » Sat Mar 18, 2006 4:07 am
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
Noobie
Forum Commoner
Posts: 85 Joined: Sun May 15, 2005 11:38 am
Post
by Noobie » Sat Mar 18, 2006 4:24 am
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
jayshields
DevNet Resident
Posts: 1912 Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England
Post
by jayshields » Sat Mar 18, 2006 6:59 am
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?
Noobie
Forum Commoner
Posts: 85 Joined: Sun May 15, 2005 11:38 am
Post
by Noobie » Sat Mar 18, 2006 7:19 am
Hi Jay - I gave it a go but still no good. The radio buttons still work - but no checked/selected automatically.
jayshields
DevNet Resident
Posts: 1912 Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England
Post
by jayshields » Sat Mar 18, 2006 7:44 am
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.
Noobie
Forum Commoner
Posts: 85 Joined: Sun May 15, 2005 11:38 am
Post
by Noobie » Sat Mar 18, 2006 8:06 am
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.
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Sat Mar 18, 2006 11:47 am
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 ';
}
?>
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sat Mar 18, 2006 1:09 pm
of note, XHTML, like the original poster appears to be using will require