Page 1 of 1
sticky Radio button
Posted: Mon Apr 06, 2009 1:45 pm
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!
Re: sticky Radio button
Posted: Mon Apr 06, 2009 2:26 pm
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

Re: sticky Radio button
Posted: Mon Apr 06, 2009 2:48 pm
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';
}
}
Re: sticky Radio button
Posted: Mon Apr 06, 2009 3:28 pm
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!

Re: sticky Radio button
Posted: Tue Apr 07, 2009 1:44 pm
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!
Re: sticky Radio button
Posted: Tue Apr 07, 2009 1:49 pm
by ninethousandfeet
actually, nevermind, i had the 'checked' outside the input tag... got it working, thank you for all of your help!