tonoslfx
tonoslfx

Reputation: 3442

jquery check radio button

PHP keeps saying unidentified index: gender? I'm not sure where the error is.

HTML:

<label>Gender:</label> <input type="radio" name="gender" value="1">Male <input type="radio" name="gender" value="0">Female

JQuery:

gender: $("input[@name=gender]:checked").val()

PHP:

$gender = $_POST['gender'];
if($gender == '') {
  echo 'Please select gender';
} 

Upvotes: 0

Views: 2188

Answers (2)

tonoslfx
tonoslfx

Reputation: 3442

sorted. thanks

gender: $('input:radio[name=gender]:checked').val()

Upvotes: 0

lonesomeday
lonesomeday

Reputation: 237865

$("input[@name=gender]:checked")

That is an invalid selector in modern jQuery. This means that no element is found, and therefore no value is set for gender. You need to remove the @ and add quote marks:

$("input[name='gender']:checked")

See the API for the attribute-equals selector.

Upvotes: 2

Related Questions