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
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

 

Link to comment
Share on other sites

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

 

 

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.