/    Sign up×
Community /Pin to ProfileBookmark

validating a radio button in php form

i am trying to create an PHP form that checks to see if a radio button as been selected before submitting information.

there are two radio buttons, and the user needs to pick one.

sorry im very new to this and any help would be very much appreciated.

Thanks you in advanced

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@ffurnaiApr 04.2005 — Explore the "if (isset($var))" conditional.

[code=php]<?php
if (isset($var)) {
echo('its checked');
} else {
echo('it ain't checked');
}
?>[/code]
Copy linkTweet thisAlerts:
@phpnoviceApr 04.2005 — ... checks to see if a radio button as been selected before submitting information. [/QUOTE]
"[b][u][i]before[/i][/u][/b]" submitting the form? That can only be accomplished with JavaScript. PHP can only look at the form [b][u][i]after[/i][/u][/b] it has already been submitted. This is OK, though, because PHP can just send it back for any needed corrections.
Copy linkTweet thisAlerts:
@ffurnaiApr 04.2005 — Good point phpnovice. ?

I missed that, mine would be on submission (to a handler page for example). Then could be sent back to the beginning with a note to complete the form in total.
Copy linkTweet thisAlerts:
@lag062authorApr 04.2005 — Explore the "if (isset($var))" conditional.

[code=php]<?php
if (isset($var)) {
echo('its checked');
} else {
echo('it ain't checked');
}
?>[/code]
[/QUOTE]


Thanks for the rapid reply, the only thing is, where would this go in the script? and i belive the $var is a choosen variable

thanks again
Copy linkTweet thisAlerts:
@lag062authorApr 04.2005 — <!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">

</head>

<body>

<form name="form1" method="post" action="">

<p>

<input name="radiobutton" type="radio" value="radiobutton">

<?php

if (isset($var)) {

echo('its checked');

} else {

echo('it ain't checked');

}

?>

</p>

<p>

<input type="submit" name="Submit" value="Submit">

</p>

</form>

</body>

</html>[/QUOTE]


this is what ive done but it dont work
Copy linkTweet thisAlerts:
@ffurnaiApr 04.2005 — OK, this is just quick but hopefully will illustrate the idea.

file one, the one with the form (I named it button.php)
[code=php]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.wx.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Button Check</title>

</head>

<body>

<form action="handler.php" method="post">
<fieldset><legend>Click a button</legend>

<p><b>button</b> <input type="radio" name="button" value="1" />

</fieldset>

<div align="center"><input type="submit" name="submit" value="Hook it up!" /></div>

</form>


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


and the "handler". The page that the form submits to, this is a checker to make sure that the form was filled out correctly (I called it handler.php).
[code=php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.wx.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Form handle</title>

</head>

<body>

<?php
if (isset($button)) {
echo('It's checked');
} else {
echo ('It ain't checked');
}
?>
</body>
</html>[/code]
×

Success!

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