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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.