Results 1 to 8 of 8

Thread: Radio Buttons

  1. #1
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1

    Unanswered: Radio Buttons

    I probably could have just asked, but there is far more satisfaction is spending an hour figuring it out for yourself.
    Selecting a default value for a radio button is easy, but none of the examples I could find showed how to set the selection to the current (database) value so that one could later edit the record.

    Code:
    FOXPRO
        61  ?"<?php "                                        
                
        71  if substr(authorized,2,1) = "A"                  
        72          ?"$ADMIN='checked="+'"checked"'+"';"     
        73          ?'$USER="";'                             
        74  else                                             
        75          ?"$USER='checked="+'"checked"'+"';"      
        76          ?'$ADMIN="";'                            
        77  endif                                            
        85 ?"?>"
    creates
    Code:
    <?php                        
        
    $AUTHORIZED="RA";            
    $ADMIN='checked="checked"';  
    $USER="";                    
    ?>
    Form
    Code:
    ...
     14   $INC=$BASEDIR."/input/".$UNIQ.".php";  
     15   include ($INC);       
    ...
        68  <li id="li_12"  class="multiple_choice two_columns">                    
        69          <label class="description">Authorization </label>               
        70          <div>                                                           
        71          <span><input id="element_12_1"  name="AUTHORIZED" class="element
     radio" type="radio" value="1" <?php echo $ADMIN ?>  />                         
        72          <label class="choice" for="element_12_1">Admin</label>          
        73          </span>                                                         
        74          <span><input id="element_12_2"  name="AUTHORIZED" class="element
     radio" type="radio" value="2" <?php echo $USER ?> />                           
        75          <label class="choice" for="element_12_2">User</label>           
        76          </span>                                                         
        77                                                                          
        78          </div>                                                          
        79  </li>

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Come again?
    George
    Home | Blog

  3. #3
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    All the examples for coding radio buttons only show how to set the default value; which is fine if you want to add a new record.
    I wanted to show the current value for an edit/update form of an existing record.

  4. #4
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Sorry dude, I think you might have skipped ahead a couple of steps. you're going to have to spoon feed me a bit more until the lightbulbs come on!

    - Radio buttons, cool. So you have a HTML form with some radio buttons...
    - Your first bit of code does a check against a variable called authorized
    - The value of this determines which radio button to be checked

    I get all that.

    Now you're talking about what to do if you've already got a value stored away in the database..?

    The logic is basically the same: just imagine you grab the value from the DB and assign it to the variable authorized... Conditionally check the box that corresponds to the value.
    George
    Home | Blog

  5. #5
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Code:
       100  <?php>
       101   $DATEFORMAT="MDY";
        102  ?>
       124                                                                         
       125  <li id="li_13"  class="multiple_choice three_columns">                 
       126          <label class="description">Date Format </label>                
       127          <div>                                                          
       128          <span><input id="element_13_1"  name="DATEFORMAT" class="elemen
     radio" type="radio" value="MDY" />                         
       129          <label class="choice" for="element_13_1">MDY</label>           
       130          </span>                                                        
       131          <span><input id="element_13_2"  name="DATEFORMAT" class="elemen
     radio" type="radio" value="DMY" />                         
       132          <label class="choice" for="element_13_2">DMY</label>           
       133          </span>                                                        
       134          <span><input id="element_13_3"  name="DATEFORMAT" class="elemen
     radio" type="radio" value="YMD"  />                         
       135          <label class="choice" for="element_13_3">YMD</label>           
       136          </span>                                                        
       137                                                                         
       138          </div>        
       139   </li>
    You mean I could have just done that: set DATEFORMAT to its current value, and that is enough to set the checked value in the radio box?

  6. #6
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Not exactly, no. You have to put the checked="checked" in.
    I would modify your code above to:
    PHP Code:
    <?php
    $DATEFORMAT
    ='MDY';
    ?>

    <li id="li_13"  class="multiple_choice three_columns">
      <label class="description">Date Format </label>
      <div>
        <span>
          <input id="element_13_1"  name="DATEFORMAT" class="elemen radio" type="radio" value="MDY" <?=($DATEFORMAT='MDY' 'checked="checked"' '')?> />
          <label class="choice" for="element_13_1">MDY</label>
        </span>
        <span>
          <input id="element_13_2"  name="DATEFORMAT" class="elemen radio" type="radio" value="DMY" <?=($DATEFORMAT='DMY' 'checked="checked"' '')?> />
          <label class="choice" for="element_13_2">DMY</label>
        </span>
        <span>
          <input id="element_13_3"  name="DATEFORMAT" class="elemen radio" type="radio" value="YMD" <?=($DATEFORMAT='YMD' 'checked="checked"' '')?> />
          <label class="choice" for="element_13_3">YMD</label>
        </span>
      </div>
    </li>
    Last edited by gvee; 02-20-15 at 06:21. Reason: consistent quoting
    George
    Home | Blog

  7. #7
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    P.S. you don't have to give every element an ID
    George
    Home | Blog

  8. #8
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Alternative method:
    PHP Code:
    <?php
      $DATEFORMAT
    ='MDY';
      
    $dateFormats = array('MDY''DMY''YMD');
    ?>
    <li class="multiple_choice three_columns">
      <div>
        <?php
          
    foreach ($dateFormats as $val) {
            
    ?>
              <label class="choice"><?=$val?>
                <input name="DATEFORMAT" type="radio" value="<?=$val?><?=$DATEFORMAT=$val 'checked="checked"' '')?> />
              </label>
            <?php
          
    }
        
    ?>
      </div>
    </li>
    There's a few extra changes in there to reduce markup (e.g. labels contain the inputs, rather than using for) but that's just personal preference. The principal is the same!
    George
    Home | Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •