Jump to content

Sticky Radio Buttons help for a noob


crunchie

Recommended Posts

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.

 

Link to comment
https://forums.phpfreaks.com/topic/261438-sticky-radio-buttons-help-for-a-noob/
Share on other sites

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

 

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

 

 

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.