sticky Radio button

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

sticky Radio button

Post by ninethousandfeet »

hello,

i have a search bar with two radio buttons, users must choose one when they search. the search results are then displayed on that same page. i am able to keep the text in the search bar when the 'Search' button is hit, but the selected radio button goes blank. does anyone know how i can keep the radio button that they selected to stay selected after the Search button is hit?

thank you!
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: sticky Radio button

Post by greyhoundcode »

You could add 'checked' to the radio tag:

Code: Select all

<input type="radio" name="selectionA" value="Crocodiles" checked />
So from PHP something along the lines of:

Code: Select all

if ($choiceA == true)
{
    $checkA = 'checked';
    $checkB = '';
}
else
{
    $checkA = '';
    $checkB = 'checked';
}
 
echo <<<HTML
<input type="radio" name="selectionA" value="Crocodiles" $checkA />
<input type="radio" name="selectionB" value="Alligators" $checkB />
HTML;
Or whatever, I'm sure you get the picture :D
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

Re: sticky Radio button

Post by ninethousandfeet »

here is what i have, looks like something similar to what you are talking about, but the proper radio button is not staying selected when i press submit... any ideas why?

Code: Select all

 
 <input name="buy_or_share"  type="radio" id="buy_or_share_0" value="share" checked="checked" />
        Buy</label>
        <label>
        <input name="buy_or_share"  type="radio" id="buy_or_share_1" value="buy" checked="checked" />
      Share (sell)</label>
    </p>
    <p>
      <label for="product_name">Product(s) and/or Store Name:</label>
      <input value="<?php if (isset($_GET['product_name'])) {
echo htmlentities($_GET['product_name'], ENT_COMPAT, 'UTF-8');}  ?>" name="product_name" type="text" id="product_name" size="40" />
    </p>
    <p>
      <input type="submit" name="search" id="search" value="Search" />
      <br />
      
    </p>
  </form>
  <p><?PHP
 
$buy = 'unchecked';
$share = 'unchecked';
 
if (isset($_GET['search'])) {
 
$selected_radio = $_GET['buy_or_share'];
 
if ($selected_radio == 'buy') {
$buy = 'checked';
}
else if ($selected_radio == 'share') {
$share = 'checked';
}
}
 
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: sticky Radio button

Post by greyhoundcode »

Well, your code is back to front.

Your code to determine which radio button should be checked ought to come at the top of the page (so first of all, set $buy and $share).

Then you need to echo $buy and $share to the screen. Currently it looks like you have hard coded things so that both radio buttons are checked (which isn't legal in any case).

Code: Select all

<?php
// You need to determine which radio is checked FIRST OF ALL
 
$buy = '';
$share = '';
 
if ( isset ( $_GET['search'] ) ) 
{
    $selected_radio = $_GET['buy_or_share'];
 
    if ($selected_radio == 'buy') 
    {
        $buy = 'checked';
    }
    elseif ($selected_radio == 'share') 
    {
        $share = 'checked';
    }
}
?>
 
<!-- This is a hard-coded approach: -->
 
<input name="buy_or_share"  type="radio" id="buy_or_share_0" value="share" checked="checked" />
 
<!-- A more flexible approach: -->
 
<input name="buy_or_share" type="radio" id="buy_or_share_0" value="share" <?php echo $share; ?> />
 
I haven't tested any of the above by the way, so once you've integrated it into your own solution you might need to debug a little! :wink:
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

Re: sticky Radio button

Post by ninethousandfeet »

okay, so this almost works... the only thing now is that the actual word 'checked' is sticking next to the selected radio button...
any ideas on how to just have the radio button checked after $_GET rather than the word 'checked'?

thank you!
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

Re: sticky Radio button

Post by ninethousandfeet »

actually, nevermind, i had the 'checked' outside the input tag... got it working, thank you for all of your help!
Post Reply