/    Sign up×
Community /Pin to ProfileBookmark

dynamically auto check radio button

i’m creating this application form that requires student’s personal details and newbie in javascript.
the intention is to auto-check the gender once user choose the relationship option accordingly;
if user choose Father, then auto-check at radio button is Male, if user choose Mother then Female radio button is checked.
I’ve created a table for that and here’s the table lokks like;

table name: RELATIONSHIP
RELCODE RELDESC RELGENDER
01 Father M
02 Mother F
03 Son M
04 Daughter F

And this is what i did so far, but unfortunately i still cannot get the right javascript function for this.
here’s the code:

[code=php]
<td>Relationship to Student </td>
<td><select name=T_RELATIONSHIP onchange=”javascript:submit_changes();”>
<?

if ($form_action == “refresh”)
{
$TT_RELATIONSHIP = $T_RELATIONSHIP;
}

$sequel = mysql_query(“SELECT RELCODE, RELDESC, RELGENDER FROM RELATIONSHIP”);
echo “<option value=”>–Choose Relationship–</option>n”;
if ($rs = mysql_fetch_array($sequel))
{
$T_RELCODE = $rs[0];
$T_RELDESC = $rs[1];
$T_RELGENDER = $rs[2];

echo “<option value=’$T_RELCODE’>$T_RELDESC</option>n”;
}
?>
</select></td>

<td>Gender </td>
<td><label><input type=”radio” name=”T_GENDER” value=”M” <? if ($T_RELCODE = “M”) { echo “checked”; }?>>Male</label>
<label><input type=”radio” name=”T_GENDER” value=”F” <? if ($T_RELCODE = “F”) { echo “checked”; } ?>>Female</label></td>

<script language=”javascript”>
function submit_changes()
{
document.FORM_RELATION.form_action.value = “refresh”;
document.FORM_RELATION.submit();
}

</script>
[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KorAug 22.2005 — As I have answered in another Forum... You may use this example:
[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function autocheck(s,o){
var r= s.form.elements['T_GENDER'];
if(o!=0){(o==1||o==3)?r[0].checked=true:r[1].checked=true;}
else{r[0].checked=r[1].checked=false;}
}
</script>
</head>
<body>
<form>
<select name="T_RELATIONSHIP" onchange="autocheck(this,this.selectedIndex)">
<option>- choose relationship -</option>
<option value='Father'>Father</option>
<option value='Mother'>Mother</option>
<option value='Son'>Son</option>
<option value='Daughter'>Daughter</option>
</select>
<input type=radio name="T_GENDER" value="M">Male</label>
<input type=radio name="T_GENDER" value="F">Female</label>
</form>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@ellzauthorAug 23.2005 — ok...i've applied to my coding and modified a bit and it works fine...thanks Kor.
×

Success!

Help @ellz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...