unable to read checkbox and radio

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sudhakararaog
Forum Newbie
Posts: 13
Joined: Thu Jan 31, 2008 1:08 am

unable to read checkbox and radio

Post by sudhakararaog »

~pickle | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


i am using a self submitting form

Code: Select all

<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST" id="test2" name="test1">
i need to do a validation of textfields, checkboxes, radio buttons

i am able to read, display and validate textfields after the form has been submitted however i am getting an error for

checkbox and radio buttons.

code for textfields

Code: Select all

username <input type="text" name="username"  value="<?php echo($username) ; ?>" />
 
$username = htmlentities($_POST["username"]);
 
if($username == "") { $error.=" enter username <br />"; }
for checkboxes and radio buttons i am getting
"Notice: Undefined index: checkboxname " for checkbox
"Notice: Undefined index: radiobuttonname " for radio button

presently the code for checkbox and radio button is

Code: Select all

<input type="checkbox" name="cbox" value="abc" />
 
$deposit = $_POST["cbox"];
 

Code: Select all

<input type="radio" name="radioname" value="one"> one
<input type="radio" name="radioname" value="two"> two

Code: Select all

$radioname = $_POST["radioname"];
i have tried cbox[] radioname[] however i keep getting
"Notice: Undefined index: cbox " and "Notice: Undefined index: radioname "
for checkbox and radio button

please advice.

thanks.


~pickle | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
kryles
Forum Contributor
Posts: 114
Joined: Fri Feb 01, 2008 7:52 am

Re: unable to read checkbox and radio

Post by kryles »

radios aren't an array though from what I recall.

$_POST['radioname'] will hold the value of whatever radio button is chosen. So get that value by

Code: Select all

 
 
$radio_var = $_POST['radioname'];
 
echo $radio_var; //will output one or two
 
 
For a checkbox, each box should have a different name. Test for it using

Code: Select all

 
 
<form blah blah blah>
<input type="checkbox" name="apple" value="apple"
<input type="checkbox" name="orange" value="orange"
<input type="submit" value="Go">
</form>
 
if (isset($_POST['apple'])) echo $_POST['apple'];
if (isset($_POST['orange'])) echo $_POST['orange']; 
 
There is a way to use checkboxes in an array but I can't recall how.

Sorry if this wasn't helpful at all =/
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: unable to read checkbox and radio

Post by Jonah Bron »

Correct, kryles.
Radio buttons do not come as arrays

$_POST is simply a super-global variable array. The error is telling you that that key is not in the array.

isset() is nessecary, as kryles showed.
Post Reply