mdvignesh Posted January 16, 2012 Share Posted January 16, 2012 I have page with male female radio button Here's the code $qry=mysql_query("SELECT * FROM reg_table where id=$id "); $res=mysql_fetch_array($qry); $radio = $res['gender']; switch($radio) { case "male": $mal = "checked"; break; case "female": $fem = "checked"; break; } Gender: <br /> <input type="radio" name="colour" value="male" checked="<?php $mal; ?>" />Male <input type="radio" name="colour" value="female" checked="<?php $fem; ?>" />Female Whats the use of checked in <input type="radio" name="colour" value="male" checked="<?php $mal; ?>" /> Quote Link to comment https://forums.phpfreaks.com/topic/255119-get-data-from-database-to-a-radio-button/ Share on other sites More sharing options...
Solution scootstah Posted January 16, 2012 Solution Share Posted January 16, 2012 It "checks" the radio button. Quote Link to comment https://forums.phpfreaks.com/topic/255119-get-data-from-database-to-a-radio-button/#findComment-1308080 Share on other sites More sharing options...
dungpt29 Posted August 14, 2013 Share Posted August 14, 2013 Try the following code: <input type="radio" name="colour" value="male" <?php if ($mal == "checked") { ?> checked <?php } ?> /> <input type="radio" name="colour" value="female" <?php if ($fem == "checked") { ?> checked <?php } ?>/> Quote Link to comment https://forums.phpfreaks.com/topic/255119-get-data-from-database-to-a-radio-button/#findComment-1444959 Share on other sites More sharing options...
PaulRyan Posted August 14, 2013 Share Posted August 14, 2013 (edited) Why keep jumping in and out of PHP tags dungpt29? Use this, as it will not produce any errors when error reporting is turned on (and it should be) <?PHP error_reporting(-1); $qry=mysql_query("SELECT * FROM reg_table where id=$id "); $res=mysql_fetch_array($qry); $radio = $res['gender']; switch($radio) { case 'male': $mal = true; break; case 'female': $fem = true; break; } ?> Gender: <br /> <input type="radio" name="colour" value="male" <?php echo isset($mal) ? 'checked' : '' ; ?> />Male <input type="radio" name="colour" value="female" <?php echo isset($fem) ? 'checked' : '' ; ?> />Female *Edit - Should the name of the check boxes be "gender" and not "colour"? Edited August 14, 2013 by PaulRyan Quote Link to comment https://forums.phpfreaks.com/topic/255119-get-data-from-database-to-a-radio-button/#findComment-1444965 Share on other sites More sharing options...
Barand Posted August 14, 2013 Share Posted August 14, 2013 The easiest way is use a loop, which is just the same code whether you have 2 or 200 options. The source for the loop can be a query or an array. For this example I'll use an array. <?php $qry=mysql_query("SELECT * FROM reg_table where id=$id "); $res=mysql_fetch_array($qry); $radio = $res['gender']; $genders = array ( 'male' => 'Male', 'female' => 'Female' ); $genderOptions = ''; foreach ($genders as $val => $text) { $chk = $val==$radio ? "checked='checked'" : ''; $genderOptions .= "<input type='radio' name='gender' value='$val' $chk />$text<br>"; } echo "Gender<br>$genderOptions"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/255119-get-data-from-database-to-a-radio-button/#findComment-1444985 Share on other sites More sharing options...
mac_gyver Posted August 14, 2013 Share Posted August 14, 2013 dungpt29, please don't bump year old threads that have been answered. the OP asked "Whats the use of checked in ..." and the first reply answered the question. Quote Link to comment https://forums.phpfreaks.com/topic/255119-get-data-from-database-to-a-radio-button/#findComment-1445035 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.