Jump to content

function to make radio button sets: stickiness not working


peppericious

Recommended Posts

I need to have several sets of radio buttons, each of which will be used to assess criteria, e.g. endurance, strength, posture, etc.

 

I've made a function, as follows:

 

<?php
function makeRadio($min, $max, $lbl) {
  echo "<div class='inline_label'>".$lbl."</div>";
  for ($i = $min; $i <= $max; $i++) 
    { 
      echo "<input type='radio' name='".$lbl."' value='".$i."' id='".$lbl.$i."_".($i-1)."'";
      if(isset($_POST[$lbl])) { echo " checked='checked'"; }
      echo "/>".$i."  "; 
    }
    echo "<br />";
}

 

I'm calling the function like this:

 

<form id="form1" name="form1" method="post" action="" >
<?php
	makeRadio(1,10, 'Endurance');
                makeRadio(1,10, 'Strength')
?>
<p><input type="submit" name="submit" id="submit" value="Submit" /></p>
</form>

 

On form submission, however, I can't get the buttons to be sticky? Where am I going wrong in my function code?

 

TIA

For a radio, you need to check the VALUE of $_POST[$lbl], not just if it's set. You are essentially setting every radio to checked, but the browser renders that as the last one checked, I'm going to guess.

Check if  $_POST[$lbl] == $i

For a radio, you need to check the VALUE of $_POST[$lbl], not just if it's set. You are essentially setting every radio to checked, but the browser renders that as the last one checked, I'm going to guess.

Check if  $_POST[$lbl] == $i

 

Thanks, jesirose. Perfect.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.