sh89 Posted November 6, 2013 Share Posted November 6, 2013 Hello all. I have tried to figure out why I can't make a certain part of my code sticky. I am attempting to save the user's input for the radio button section. I am using a foreach loop for everything in my array. When I try to use if isset, it returns the last item in the array instead of what was selected by the user. Here is the code: <?php //Creating foreach loop for magazine selection foreach ($magSubs as $mag => $SUB_PRICE) { echo "<input type='radio' name='magtype' value='$mag' />$mag $$SUB_PRICE per month<br> "; } ?> Again, I want to make this part sticky. Quote Link to comment https://forums.phpfreaks.com/topic/283636-making-radio-buttons-sticky-in-a-foreach-array-loop/ Share on other sites More sharing options...
Rifts Posted November 6, 2013 Share Posted November 6, 2013 try addiing checked="checked" to your input Quote Link to comment https://forums.phpfreaks.com/topic/283636-making-radio-buttons-sticky-in-a-foreach-array-loop/#findComment-1457135 Share on other sites More sharing options...
sh89 Posted November 6, 2013 Author Share Posted November 6, 2013 I tried that, but unfortunately all that does is check the last item in the array. I also tried that with if isset, to no avail. Quote Link to comment https://forums.phpfreaks.com/topic/283636-making-radio-buttons-sticky-in-a-foreach-array-loop/#findComment-1457137 Share on other sites More sharing options...
sh89 Posted November 6, 2013 Author Share Posted November 6, 2013 This is what I've come up with, but I still have the issue of it checking the last item in the array and not the button that was selected: <?php //Creating foreach loop for magazine selection foreach ($magSubs as $mag => $SUB_PRICE) { echo "<input type='radio' name='magtype' value='$mag' "; if (isset($_GET['magtype'])) { echo "checked='checked'"; } echo " >$mag $$SUB_PRICE per month<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/283636-making-radio-buttons-sticky-in-a-foreach-array-loop/#findComment-1457139 Share on other sites More sharing options...
Solution DavidAM Posted November 6, 2013 Solution Share Posted November 6, 2013 Please use tags. For multiple radio buttons, the HTML name attribute needs to represent/produce an array. name="magtype[]". In your loop, you need to check for the value in the array: //Creating foreach loop for magazine selection foreach ($magSubs as $mag => $SUB_PRICE) { echo "<input type='radio' name='magtype[]' value='$mag' "; if (isset($_GET['magtype'])) { if (in_array($mag, $_GET['magtype'])) { echo "checked='checked'"; } } echo " >$mag $$SUB_PRICE per month<br>"; // WHAT IS ^^ THIS? Is that supposed to be a variable-variable? } Quote Link to comment https://forums.phpfreaks.com/topic/283636-making-radio-buttons-sticky-in-a-foreach-array-loop/#findComment-1457259 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.