peppericious Posted April 26, 2012 Share Posted April 26, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/261655-function-to-make-radio-button-sets-stickiness-not-working/ Share on other sites More sharing options...
Jessica Posted April 26, 2012 Share Posted April 26, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/261655-function-to-make-radio-button-sets-stickiness-not-working/#findComment-1340785 Share on other sites More sharing options...
peppericious Posted April 26, 2012 Author Share Posted April 26, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/261655-function-to-make-radio-button-sets-stickiness-not-working/#findComment-1340796 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.