Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Radio Input throwing Undefined Index

Posted on 2006-07-07
9
Medium Priority
?
388 Views
Last Modified: 2008-03-10
Hi,

I have a form that submits values from the following radio button:

<td height="185" align="left" valign="top">
<p><font face="Tahoma" size="2">How many people (on average) will be watching this presentation? <br>
<input type="radio" name="Watching" value="Less than 5">
Less than 5 <br>
<input type="radio" name="Watching" value="6 to 15">
6 to 15<br>
</font><font face="Tahoma" size="2">
<input type="radio" name="Watching" value="16 to 30">
16 to 30 <br>
<input type="radio" name="Watching" value="31 to 100">
31 to 100 <br>
<input type="radio" name="Watching" value="101 to 500">
101 to 500 <br>
<input type="radio" name="Watching" value="More than 500">
More than 500</font></p></td>

I then submit the code to another page where I attempt to verify it:

function cleantext($text)
{
      if( isset( $text )){
            return $text;
      } else {
            $r = "";
            return $r;
      }
}

//Submission conversion
$Watching = cleantext($_POST["Watching"]);


But i get the error:

Notice: Undefined index: Watching in C:\Sites\Single27\bsowards\webroot\presteam\Services\Packaged\3 Templates\formsubmit.php on line 49


I'm using PHP 5 on Windows Server 2003, can anyone tell me how to check if a Radio submitted value is valid? is it because it is an array? Sample code would be much appreciated thanks!

0
Comment
Question by:bsowards
  • 4
  • 4
9 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 17057900
You can use below code, to debug your request variables:
echo '<pre>'.var_export($_POST,true).'</pre>';

-r-
0
 

Author Comment

by:bsowards
ID: 17057980
your debug did not provide any information or did not work. I'm still getting the error with no additional information.
0
 

Author Comment

by:bsowards
ID: 17057990
whoops! put at end of code so didn't get to it.

It's showing that the value "Watching" isn't even included in the index, which is what I assumed from the error. So how come my detection script isn't handling that?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 49

Expert Comment

by:Roonaan
ID: 17058074
Are the radio buttons between your <form> and </form> tags?

-r-
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 17058076
Change $_POST to $_GET.

This should match the form's action (POST or GET).

Can you show the complete form you are using?

0
 

Author Comment

by:bsowards
ID: 17058113
I don't want to use $_GET, I prefer $_POST.

The form works fine when one of the radio buttons are selected, but doesn't if one isn't. Most sites recommend setting one radio button to the default by putting "checked" on one of the options.

I'd prefer to know how to check if a variable is IN the index, that way I don't have to worry about it. Thanks.
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 17058135
if(isset($_POST['Watching']))

-r-
0
 

Author Comment

by:bsowards
ID: 17058300
Yes well, my function is set to do that, but it doesn't work.

function cleantext($text)
{
     if( isset( $text )){
          return $text;
     } else {
          $r = "";
          return $r;
     }
}

//Submission conversion
$Watching = cleantext($_POST["Watching"]);
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 2000 total points
ID: 17058319
No, your function isn't. You cannot pass a non-existing variable and then test if it is set.

So you would use:
$Watching = isset($_POST['Watching']) ? $_POST['Watching'] : '';

Or
$Watching = cleantext($_POST, $Watching)
And use:
function cleantext($array, $key) {
  if(isset($array[$key])) {
  return $array[$key];
  } else { return '';}
}

-r-
}

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question