1

I'm working on a bit of code to edit data from a database. On the original form (which I am mimicking) it has a radio button group with 6 options for days :

 <label for="arrival">When will you arrive?</label><br />
    <input type="radio" id="pre" name="arrival" value="pre"/>Pre-KG<br />
    <input type="radio" id="wed" name="arrival" value="wed"/>Wednesday<br />
    <input type="radio" id="thu" name="arrival" value="thu"/>Thursday<br />
    <input type="radio" id="fri" name="arrival" value="fri"/>Friday<br />
    <input type="radio" id="sat" name="arrival" value="sat"/>Saturday<br />
    <input type="radio" id="sun" name="arrival" value="sun"/>Sunday<br />

I the put the value to the database after submission of the form. Now I am calling the data from the database and I want to have the correct radio button selected. Any suggestions on this? I can't seem to find it by searching...

values are coming from an sql query to my database using $row["arrival"] with a value of one of the values above...

Previously for a yes/no option I used...

if ($row["costyn"]==1){
    echo '<input type="radio" name="costyn" value="1" checked>Yes<br>';
    echo '<input type="radio" name="costyn" value="0">No<br>';
    } else if ($row["costyn"]==0) {
    echo '<input type="radio" name="costyn" value="1" >Yes<br>';
    echo '<input type="radio" name="costyn" value="0" checked>No<br>';
    }

There must be a cleaner way though.

5
  • 2
    Side note, IDs must be unique. Commented Nov 18, 2014 at 16:43
  • Where are you retrieving the value to match? Commented Nov 18, 2014 at 16:44
  • you need to put selected = selected on the one you've on the db Commented Nov 18, 2014 at 16:45
  • @MarcoMura For radio buttons it's checked, not selected. Commented Nov 18, 2014 at 16:47
  • @caCtus Right, too much work today o.o however if this radio isn't inside a loop he need to put a php if on each line or a Javascript that will "check" the correct radio Commented Nov 18, 2014 at 16:48

2 Answers 2

2

In such case something like this may do the job:

<label for="arrival">When will you arrive?</label><br />
<?
$row["costyn"] = 4; // value from database

$radio_keys= array('pre', 'wed', 'thu', 'fri', 'sat', 'sun');
$radio_values = array('Pre-KG', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');

foreach ($radio_keys as $key => $value) {
   echo '<input type="radio" name="arrival" value="'.$value.'" '.(($key+1)==$row["costyn"]?'checked':'').'/>'.$radio_values[$key].'<br />';
}
//ommited the ID, if you need it for JS/CSS reference you shall make it unique or use name/class for whole group.
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this worked like a charm and made total sense to me. Appreciated!
Thanks, I updated the script for the output from database being a "number"
0

Your PHP

firstly get which one you want to be checked then do this

lets say you have it as "pre"

$radio = "pre";

then

$checked[$radio] = "checked";

Your HTML

<label for="arrival">When will you arrive?</label><br />
<input type="radio" id="arrival1" name="arrival" value="pre" <?=(isset($checked['pre']))?$checked['pre']:"";>/>Pre-KG<br />
<input type="radio" id="arrival2" name="arrival" value="wed" <?=(isset($checked['wed']))?$checked['pre']:"";>/>Wednesday<br />
<input type="radio" id="arrival3" name="arrival" value="thu" <?=(isset($checked['thu']))?$checked['pre']:"";>/>Thursday<br />
<input type="radio" id="arrival4" name="arrival" value="fri" <?=(isset($checked['fri']))?$checked['pre']:"";>/>Friday<br />
<input type="radio" id="arrival5" name="arrival" value="sat" <?=(isset($checked['sat']))?$checked['pre']:"";>/>Saturday<br />
<input type="radio" id="arrival6" name="arrival" value="sun" <?=(isset($checked['sun']))?$checked['pre']:"";>/>Sunday<br />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.