/    Sign up×
Community /Pin to ProfileBookmark

Form Validation (Radio Buttons…)

Hi Guys,

I have created a very simple form with a text box and radio buttons. I am using javascript to echo my php in a div but it won’t allow me to see which radio button was selected! Nightmare… 🙁

[code=html]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ />
<title>Untitled Document</title>
</head>

<body>

<form id=”testForm” method=”post” >

<input type=”text” name=”info” >info<br>
<input type=”radio” name=”sex” value=”male”>Male<br>
<input type=”radio” name=”sex” value=”female”>Female

<input type=”submit” value=”Submit”>
</form>

<div id=”response”>
dafadf
<div class=”clearer”></div><!–clearer–>

</div><!–response–>
<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js” type=”text/javascript”></script>
<script type=”text/javascript” src=”js/testForm.js”></script>

</body>
</html>[/code]

[CODE]testForm.js file

$(document).ready(function()
{
$(“#testForm”).submit(function()
{

var sex_Js = $(“#sex”).val();

var info_Js = $(“#info”).val();

$(“#response”).load(“../testPhp.php”,{

sex:sex_Js,

info:info_Js

});

return false;
});

});[/CODE]

[code=php] <?php

$nameField_PHP = $_POST[‘sex’];

$info_PHP = $_POST[‘info’];

echo $info_PHP;
echo “<br />” . $nameField_PHP;

?> [/code]

Why isn’t the javascript sending the radio buttons value through to the PHP?

Many thanks.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@cbVisionOct 01.2012 — You are targeting ID's, but the inputs don't have ID's ... they are named.

var sex_Js = $("#sex").val();

var info_Js = $("#info").val();

What you want to do is find the radio button that is checked and get the value of that:

var sex_Js = $('input[name=sex]:checked').val();
Copy linkTweet thisAlerts:
@mikex2321authorOct 01.2012 — Awesome! Works a treat.

One slight problem I now: How do I apply a value to the radio buttons if they're unchecked..? So in the PHP file I'll tell it to echo 'Blah blah blah' if male is selected, or 'Yes yes yes' if female is selected, BUT if they don't select either radio button for it to echo 'Please choose a gender'.

Many thank for your help!
Copy linkTweet thisAlerts:
@xelawhoOct 01.2012 — afaik, a radio button needs to be selected for its value to be sent to the server.

you could get around this by having another RB in the group, hidden and selected so that if another one isn't selected that value gets sent:

[CODE]<input type="radio" name="sex" style="display:none" checked="checked" value="Please choose a gender">[/CODE]
Copy linkTweet thisAlerts:
@mikex2321authorOct 02.2012 — You. Are. Awesome!

Thank you so much for your help!
×

Success!

Help @mikex2321 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.6,
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,
)...