Reputation: 63
Creating a radio button set based off of PHP/MySQL
Having an issue with a button being checked on default: What it looks like:
The code:
$first = TRUE;
if (mysql_num_rows($result))
{
for ($j = 0; $j < mysql_num_rows($result); $j++)
{
$currentCat = mysql_result($result, $j, 'category');
if ($first == TRUE)
{
$first = FALSE;
echo "<input type='radio' name='createCat' value='$currentCat' checked='checked' />$currentCat checked";
}
else
{
echo "<input type='radio' name='createCat' value='$currentCat' />$currentCat ";
echo "non";
}
}
echo <<<_END
<br /><input type='submit' value='Create' /> $error
</form>
_END;
}
Notice how the first box isnt checked The output in HTML:
<input type='radio' name='createCat' value='opt1' checked='checked' />opt1 checked<input type='radio' name='createCat' value='opt2' />opt2 non<input type='radio' name='createCat' value='Accounts' />Accounts non <br /><input type='submit' value='Create' />
Upvotes: 0
Views: 10302
Reputation: 11
Change your first option to look like this and it should default to checked:
echo "<input type='radio' name='createCat' value='$currentCat' checked/>$currentCat checked";
Notice that you have
checked="checked"
Removing the ="checked" portion will fix the problem.
Upvotes: 1