By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,082 Members | 2,102 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,082 IT Pros & Developers. It's quick & easy.

unable to read checkbox and radio

P: 54
i am using a self submitting form
[php]
<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"];
[/php]
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.

Use the appropriate code tags when showing any code in this forum! See the Posting Guidelines before you continue - moderator
Feb 29 '08 #1
Share this Question
Share on Google+
2 Replies


hsriat
Expert 100+
P: 1,654
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>";
print_r($_POST);
echo "</pre>";
?>[/php]

This may help you solve your problem.
Feb 29 '08 #2

100+
P: 103
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" />
  2.  
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. ?>
  11.  
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;
  2.  
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.