Johnson
Johnson

Reputation: 818

Form with radio button

I have this:

<td>
    <input id="sex" name="sexFemale" value="female" type="radio">
    <label for="sexFemale">
        Kvinna
    </label>
</td>
<td>
    <input id="sex" name="sexBoth" value="both" checked="checked" type="radio">
    <label for="sexBoth">
        Båda
    </label>
</td>
<td>
    <input id="sex" name="sexMale" value="male" type="radio">
    <label for="sexMale">
        Man
    </label>
</td>

I think I made this wrong, how should I use it?

$_POST["sex"] to get the value "male" or "female" or what they chosed

Upvotes: 1

Views: 276

Answers (6)

poke
poke

Reputation: 388313

Just switch your name attributes with your id attributes. So instead of

<input id="sex" name="sexFemale" ...

just use

<input id="sexFemale" name="sex" ...

Upvotes: 0

Randy the Dev
Randy the Dev

Reputation: 26740

You need to give them the same name (the name of the radio button group) attribute and different id attributes to work:

<td>
  <label><input id="sexFemale" name="groupSex" value="female" type="radio">
  Kvinna</label>
</td>
<td>
  <label><input id="sexBoth" name="groupSex" value="both" checked="checked" type="radio">
  Båda</label>
</td>  
<td>
  <label><input id="sexMale" name="groupSex" value="male" type="radio">
  Man</label>
</td>

Upvotes: 1

Drakmail
Drakmail

Reputation: 1

$_POST['sex'] returns are element value with name attribute "sex". For use it - set all radiobutons name to "sex".

Upvotes: 0

heximal
heximal

Reputation: 10517

i think than all radio button inputs in one group must have the same 'name' attribute:

Kvinna Båda
Man

Upvotes: 0

oezi
oezi

Reputation: 51817

the name of the radiobuttons has to be the same, not the id (the id must be unique)

Upvotes: 1

kender
kender

Reputation: 87261

You should set their name attribute to "sex" and have them carry diffrerent values:

<input type="radio" name="sex" value="female" />Female<br />
<input type="radio" name="sex" value="male" />Male<br />

Upvotes: 2

Related Questions