I have read a lot of posts here and elsewhere about remembering radio buttons. I have also read a lot of posts about initialising one radio button. What I have not read a lot of is combining the two. I am wanting to do it within the input statement if possible.
Examples of remembering (ie. retaining the highlighted radio button on re-display of the page):
<input type="radio" name="sex" value="Male" <?php echo ($sex=='Male')?'checked':'' ? >size="17">Male
<input type="radio" name="sex" value="Female" <?php echo ($sex=='Female')?'checked':'' ?> size="17">Female
or
<input type="radio" name="veg" value="cabbage" <?php if(!isset($veg)){print " checked=\"checked\"";} if(isset($veg) && $veg == "cabbage"){print " checked=\"checked\"";} ?>> cabbage
<input type="radio" name="veg" value="onion" <?php if(isset($veg) && $veg == "onion"){print " checked=\"checked\"";} ?>> onion
or
<input name="searchtype" type="radio" value="artist"
<? if ($searchtype == 'artist') echo 'checked'; ?>>
Title:
<input name="searchtype" type="radio" value="title"
<? if ($searchtype == 'title') echo 'checked'; ?>>
Year:
<input name="searchtype" type="radio" value="year"
<? if ($searchtype == 'year') echo 'checked'; ?>>
The first one seems simplest, but I cannot get it to work. I keep getting some error, presumably syntax. Here is my partial code:
<input type="radio" name="typ" value="exact" <?php if ($_GET["typ"]=="exact") echo "checked=\"checked\""; ?>Exact
<input type="radio" name="typ" value="starts" <?php if ($_GET["typ"]=="starts") echo "checked=\"checked\""; ?>Starts with
<input type="radio" name="typ" value="sounds" <?php if ($_GET["typ"]=="sounds") echo "checked=\"checked\""; ?>Soundex</td></tr>
<input type="submit" value="Find" /></td></tr>
The second issue is incorporating the default value into the above somehow.
My attempt involved using the third format of the code above and trying to test for null initially. Something like this. Once again it did not work:
Exact
Starts with
Soundex
The following code works, but is verbose:
$exact_status = 'unchecked';
$starts_status = 'unchecked';
$sounds_status = 'unchecked';
if (isset($_POST['typ'])) {
$selected_radio = $_POST['typ'];
if ($selected_radio == 'exact')
{$exact_status = 'checked';}
else if ($selected_radio == 'starts')
{$starts_status = 'checked';}
else if ($selected_radio == 'sounds')
{$sounds_status = 'checked';}
}
else {
$exact_status = 'checked';}
echo
'<p>Enter Surname and optionally Given Name(s):
<form action="'.$_SERVER['PHP_SELF'].'" method="post">
<table cellpadding="4" cellspacing="0">
<tr><td valign="top">Surname:</td><td><input type="text" size=40 name="surname" value="'.$surname.'" /></td><td>Required</td></tr>
<tr><td valign="top">Given Name(s):</td><td><input type="text" size=40 name="firstname" value="'.$firstname.'" /></td><td>Optional</td></tr>
<tr><td valign="top">Search Type:</td><td>
<input type="radio" name="typ" value="exact"'.$exact_status.'>Exact
<input type="radio" name="typ" value="starts"'.$starts_status.'>Starts with
<input type="radio" name="typ" value="sounds"'.$sounds_status.'>Soundex
</td></tr>
<tr><td></td><td><input type="submit" value="Find" /></td></tr>
</table>
</form>
<p>';
Based on comments below I tried this. It almost works. The first input box works. The next two just output the word "checked" instead of the input statement???
if (!isset($_POST['typ']))
{$radio1 = '<input type="radio" name="typ" value="exact" checked>Exact';}
else
{$radio1 = '<input type="radio" name="typ" value="exact"'.($_GET['typ'] =="exact")?"checked":''.'>Exact';}
$radio2 = '<input type="radio" name="typ" value="starts"'.($_GET['typ'] =="starts")?"checked":''.'>Starts with';
$radio3 = '<input type="radio" name="typ" value="sounds"'.($_GET['typ'] =="sounds")?"checked":''.'>Soundex';
echo
'<p>Enter Surname and optionally Given Name(s):
<form action="'.$_SERVER['PHP_SELF'].'" method="post">
<table cellpadding="4" cellspacing="0">
<tr><td valign="top">Surname:</td><td><input type="text" size=40 name="surname" value="'.$surname.'" /></td><td>Required</td></tr>
<tr><td valign="top">Given Name(s):</td><td><input type="text" size=40 name="firstname" value="'.$firstname.'" /></td><td>Optional</td></tr>
<tr><td valign="top">Search Type:</td><td>',$radio1,$radio2,$radio3,
'</td></tr>
<tr><td></td><td><input type="submit" value="Find" /></td></tr>
</table>
</form>
<p>';
In other words after the first radio button it says: Exactcheckedchecked. The next two buttons are missing.