crunchie Posted April 22, 2012 Share Posted April 22, 2012 Hey all, I'm somewhat new to PHP and working on a form and trying to make radio buttons sticky. I've tried the following but when I refresh/got back to the page then now matter what button I chose it always shows the "4" bedroom or "No". What size of unit do you require? <input type="radio" name="unitsize" value="1 Bedroom" <?php if (isset($_POST['unitsize']) == '1 Bedroom') echo 'checked'; ?>> 1 bedroom <input type="radio" name="unitsize" value="2 Bedroom" <?php if (isset($_POST['unitsize']) == '2 Bedroom') echo 'checked'; ?>> 2 bedroom <input type="radio" name="unitsize" value="3 Bedroom" <?php if (isset($_POST['unitsize']) == '3 Bedroom') echo 'checked'; ?>> 3 bedroom <input type="radio" name="unitsize" value="4 Bedroom" <?php if (isset($_POST['unitsize']) == '4 Bedroom') echo 'checked'; ?>> 4 bedroom Do you require an accessible unit?</td> <input type="radio" name="accessible" value="Yes" <?php if (isset($_POST['accessible']) == 'Yes') echo 'checked'; ?>> Yes <input type="radio" name="accessible" value="No" <?php if (isset($_POST['accessible']) == 'No') echo 'checked'; ?>> No Any help or suggestions are greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/261438-sticky-radio-buttons-help-for-a-noob/ Share on other sites More sharing options...
wigwambam Posted April 22, 2012 Share Posted April 22, 2012 Try it without the isset <input type="radio" name="unitsize" value="1 Bedroom" <?php if ($_POST['unitsize'] == '1 Bedroom') echo 'checked'; ?>> 1 bedroom <input type="radio" name="unitsize" value="2 Bedroom" <?php if ($_POST['unitsize'] == '2 Bedroom') echo 'checked'; ?>> 2 bedroom <input type="radio" name="unitsize" value="3 Bedroom" <?php if ($_POST['unitsize'] == '3 Bedroom') echo 'checked'; ?>> 3 bedroom <input type="radio" name="unitsize" value="4 Bedroom" <?php if ($_POST['unitsize'] == '4 Bedroom') echo 'checked'; ?>> 4 bedroom Quote Link to comment https://forums.phpfreaks.com/topic/261438-sticky-radio-buttons-help-for-a-noob/#findComment-1339654 Share on other sites More sharing options...
gizmola Posted April 22, 2012 Share Posted April 22, 2012 Yes, isset() returns true and false, not the value of the variable. If you wanted to combine them you would do: if (isset($_POST['unitsize']) && $_POST['unitsize'] == 'string') echo 'checked'; A couple of tips: php alternative syntax is nice for this type of inline mixing of markup and code when all you want to do is echo out a simple value, and the ternary operator is great for replacing if then else logic (even though you don't really need the else here). > 1 bedroom Quote Link to comment https://forums.phpfreaks.com/topic/261438-sticky-radio-buttons-help-for-a-noob/#findComment-1339671 Share on other sites More sharing options...
crunchie Posted April 25, 2012 Author Share Posted April 25, 2012 Thank you guys very much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/261438-sticky-radio-buttons-help-for-a-noob/#findComment-1340442 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.