PHP – Simple Generic Radio Buttons Function

The given PHP example helps you to make the radio buttons with the generic function.

Code Example:
<?php
function makeRadioButtons($name,$current,$values=array(1,0),$prompt=array("Yes","No")) { 
  // $name      button set name 
  // $current   current ("checked") value 
  // $value     (optional) array of possible values, default 1 and 0 
  // $prompt    (optional) array of possible prompts, default yes and no 
  
  $s = "";
  $c = count($prompt); 
  for($i=0; $i<$c; $i++) { 
    $s .= '<input type="radio" name="'.$name.'" value="'.$values[$i].'"'; 
    $s .= ($values[$i]==$current) ? ' checked="checked" />':' />'; 
    $s .= $prompt[$i]."<br />\n"; 
  } 
  return $s; 
} // end function MakeRadioButtons 

// Example 

$defaultButtons = makeRadioButtons('yesNo',$yesNo); 

$languageValues = array('',        // initial empty value 
                'fr_en', 
                'de_en', 
                'it_en', 
                'ko_en', 
                'pt_en', 
                'ru_en', 
                'es_en' 
                ); 

$languagePrompts = array('None', 
                'French to English', 
                'German to English', 
                'Italian to English', 
                'Korean to English', 
                'Portuguese to English', 
                'Russian to English', 
                'Spanish to English' 
                ); 

$languageButtons = makeRadioButtons('language',$language,$languageValues,$languagePrompts); 

$msg = ($update)? '<p>You chose: '.$language.'</p>':'<p>Choose a language</p>'; 

echo "
$msg 
<form method='post' action=''> 
<input type='hidden' name='update' value='1'> 
<p>Translate?<br /> 
$defaultButtons</p> 
<p>Choose Language<br /> 
$languageButtons</p> 
<input type='submit' value='Submit'> 
";
?>

Post to Twitter Post to Digg Post to Facebook Post to Google Buzz Send Gmail

Leave a Comment

Your email address will not be published. Required fields are marked *