469,890 Members | 2,154 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,890 developers. It's quick & easy.

unable to read checkbox and radio

i am using a self submitting form
<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

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

<input type="checkbox" name="cbox" value="abc" />

$deposit = $_POST["cbox"];

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

$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.


Use the appropriate code tags when showing any code in this forum! See the Posting Guidelines before you continue - moderator
Feb 29 '08 #1
2 2588
1,654 Expert 1GB
Although I didn't get what you are trying to ask. But I'll tell you a good way to debug such things.

In the file, whom you have set as the value of action attribute in form tab, comment all the code and add this in the starting (just temporarily).[php]<?php
echo "<pre>";
echo "</pre>";

This may help you solve your problem.
Feb 29 '08 #2
103 100+
I reckon the error is caused because your form doesn't know if it's been submitted or not...

Try adding something like:

Expand|Select|Wrap|Line Numbers
  1. <input type="hidden" name="submitted" value="true" />
inside the form tags.

Then at the TOP (ie before the form-display itself) put something like:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if (isset($_POST['submitted'])) {
  3.   $radioname= $_POST['radioname'];
  4.   //now do something with this value...
  5.   echo $radioname;
  6. }
  7. else {
  8.   $radioname=NULL;
  9. }
  10. ?>
This way if you reference $radioname, you won't get the notice about the missing index when you first load the page (and the $_POST array has nothing in it). You can avoid all notices like this by using "isset " on the variable before using it. IE:

Expand|Select|Wrap|Line Numbers
  1. $radioname = (isset($_POST['radioname'])) ? $_POST['radioname'] : NULL;
Something to note too - when you use self-submitting forms, if you hit refresh on your browser, the form will resubmit so normally you have to keep a track of whether it's been submitted - I use sessions, you can use DB too...
Feb 29 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Arun Nair | last post: by
1 post views Thread by John Mullen | last post: by
4 posts views Thread by Steph | last post: by
2 posts views Thread by /.. | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.