missing information error...undefined

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
timwhelan
Forum Newbie
Posts: 8
Joined: Mon Sep 18, 2006 11:34 am

missing information error...undefined

Post by timwhelan »

Okay I am just getting back into php and mysql, so I am having dumb questions.

I have a form with radio buttons in it. If someone doesn't check it I am getting an error.
Notice: Undefined index: newsubscience in /www/content/nasa/html/_test/live_test/signup.php on line 213

Code: Select all

 
<tr>
        <td align="right" style="vertical-align:top;"><p>Subject Areas Taught</p></td>
        <td  style="vertical-align:top;">
        <input name="newsubmath" value="Math" type="radio">Math<br />
        <input name="newsubscience" value="Science" type="radio">Science<br />
        <input name="newsubsocial" value="SocialScience" type="radio">Social Science<br />
        <input name="newsublang" value="Languagearts" type="radio">Language Arts<br />
        <input name="newsubtech" value="Technology" type="radio">Technology<br />
        <input name="newsubspecial" value="Specialed" type="radio">Special Ed <br />
        <input name="newsubgifted" value="Gifted" type="radio">Gifted<br />
        <input name="newsubother" value="Other" type="radio">Other<br />
        </td>
    </tr> 
 
Each one has its own cell in the table.
I don't want the error. I am not expecting someone to check it if it isn't relavant.

What Am I not thinking of here?

Thanks!
Tim
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: missing information error...undefined

Post by Mark Baker »

Example radio button from W3Schools

Code: Select all

 
<input type="radio" name="sex" value="male" /> Male
<br />
<input type="radio" name="sex" value="female" /> Female
<br />
 
Can you see the difference between how a radio button should be defined in HTML, and your radio button?

Clue: Pay particular attention to the name attribute
timwhelan
Forum Newbie
Posts: 8
Joined: Mon Sep 18, 2006 11:34 am

Re: missing information error...undefined

Post by timwhelan »

Okay, I do get that. Thank you.

So, now my question is how to set these up as individual inputs without getting an error. Checkbox?
If not check box how would I get a single entry without getting the error that it was left blank?

Thanks!!!
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: missing information error...undefined

Post by superdezign »

Use empty() to help validate information. Always validate user input before attempting to use it.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: missing information error...undefined

Post by Mark Baker »

timwhelan wrote:So, now my question is how to set these up as individual inputs without getting an error. Checkbox?
If not check box how would I get a single entry without getting the error that it was left blank?
The input type you use depends what you want to allow your users to select.
Radio Buttons permit the selection of one only from a series of input fields, grouped together by having a common name (e.g. the gender example that I quoted from W3Schools, where only one of the two input options can be valid at any one time.

Checkboxes basically allow a series of Yes/No options, where users can check the box to indicate yes, or leave it blank for no
Again, an example from W3Schools

Code: Select all

 
I have a bike:
<input type="checkbox" name="bike" value="Bike" />
<br />
I have a car:
<input type="checkbox" name="car" value="Car" />
<br />
I have an airplane:
<input type="checkbox" name="plane" value="Airplane" />
<br />
<input type="submit" name="submit" value="Submit" />
 
In this case, the request will contain only those names and values that were checked when the user submits

Code: Select all

 
$_POST Array
(
    [bike] => Bike
    [car] => Car
    [submit] => Submit
)
 
 
As an alternative, you can name your checkboxes using an array

Code: Select all

 
I have a bike:
<input type="checkbox" name="vehicle[]" value="Bike" />
<br />
I have a car:
<input type="checkbox" name="vehicle[]" value="Car" />
<br />
I have an airplane:
<input type="checkbox" name="vehicle[]" value="Airplane" />
<br />
<input type="submit" name="submit" value="Submit" />
 
but it will still only include those name/value pairs that were checked

Code: Select all

 
$_POST Array
(
    [vehicle] => Array
        (
            [0] => Bike
            [1] => Car
        )
 
    [submit] => Submit
)
 
It's up to you, as the developer, to identify unchecked values
Post Reply