Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4323528
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:00:52+00:00 2026-05-21T09:00:52+00:00

I have just started learning and have put together the form below. The confusion

  • 0

I have just started learning and have put together the form below. The confusion I have is when I use empty to validate if the user selected or entered any information, the code generates the error below.

I noticed if I have the following lines of code

if(empty($gender)) {
        $errormessage[2] = "Please select your gender";
    } 

as

if(empty($_POST["gender"])) {
        $errormessage[2] = "Please select your gender";
    }

and

if(empty($gender)) {
        $errormessage[2] = "Please select your gender";
    }

as

if(empty($_POST["gender"])) {
        $errormessage[2] = "Please select your gender";
    }

I do not see the error message. I take it that error message is being generated because of the lines of code $_POST for the gender and media form elements which are radio buttons and checkboxes respectively. However if I want to initialize all the variables at the top, what is the best way to do so?

/* <?php

if(isset($_POST["submit"])) {

    $fname = $_POST["fname"];
    $lname = $_POST["lname"];
    $gender = $_POST["gender"];
    $age = $_POST["age"];
    $address = $_POST["address"];
    $media = $_POST['media'];

    $errormessage =  array();

    if(empty($fname)) {
        $errormessage[0] = "Please enter your first name";
    }

    if(empty($lname)) {
        $errormessage[1] = "Please enter your last name";
    }

    if(empty($gender)) {
        $errormessage[2] = "Please select your gender";
    }

    if(empty($age)) {
        $errormessage[3] = "Please select your age";
    }

    if(empty($address)) {
        $errormessage[4] = "Please enter your address";
    }

    if(empty($media)) {
        $errormessage = "Please select the type of media";

    }

}
?>
<html>
<head>
    <title>Sample Registration</title>
</head>
<body>
    <h2>Sample registration</h4>
        <form name="registration" method="post" action="registration.php">
            <div>
                First Name: <br />
                <input type="text" name="fname" value="">
            </div>

            <div>
                Last Name: <br />
                <input type="text" name="lname" value="">
            </div>

            <div>
                Gender: <br />
                male<input type="radio" name="gender" value="male">
                female<input type="radio" name="gender" value="female">
            </div>

            <div>
                Age: <br />
                <select name="age">
                    <option value="">Please select your age</option>
                    <option value="18-25">18-25</option>
                    <option value="26-33">26-33</option>
                </select>
            </div>

            <div>
                Address: <br />
                <textarea name="address" cols="10" rows="10"></textarea>
            </div>

            <div>
                Sign-me up: <br />
                <input type="checkbox" name="media['newsletter']" value="newsletter"> newsletter
                <input type="checkbox" name="media['specials']" value="specials"> specials
                <input type="checkbox" name="media['events']" value="events"> events

            <div>
                <input type="submit" name="submit" value="submit">
            </div>
        </form>
</body>
</html>

*/

/* Error Message */

! ) Notice: Undefined index: gender in C:\Program Files\EasyPHP-5.3.5.0\www\registration.php on line 7
Call Stack
#   Time    Memory  Function    Location
1   0.0004  341792  {main}( )   ..\registration.php:0
Dump $_SERVER

$_SERVER['REMOTE_ADDR'] =



string '127.0.0.1' (length=9)

$_SERVER['REQUEST_METHOD'] =



string 'POST' (length=4)

$_SERVER['REQUEST_URI'] =



string '/registration.php' (length=17)

Variables in local scope (#1)

$address =

    Undefined

$age =

    Undefined

$errormessage =

    Undefined

$errormsg =

    Undefined

$fname =



string '' (length=0)

$gender =

    Undefined

$lname =



string '' (length=0)

$media =

    Undefined

( ! ) Notice: Undefined index: media in C:\Program Files\EasyPHP-5.3.5.0\www\registration.php on line 10
Call Stack
#   Time    Memory  Function    Location
1   0.0004  341792  {main}( )   ..\registration.php:0
Variables in local scope (#1)

$address =



string '' (length=0)

$age =



string '' (length=0)

$errormessage =

    Undefined

$errormsg =

    Undefined

$fname =



string '' (length=0)

$gender =



null

$lname =



string '' (length=0)

$media =

    Undefined
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T09:00:52+00:00Added an answer on May 21, 2026 at 9:00 am

    You’ll want to check whether those $_POST['gender'] and $_POST['media'] variables were actually set in the HTTP POST request before you go accessing them; a better solution to initialize each $_POST var might be something like this:

    $fname = isset( $_POST['fname'] ) ? $_POST['fname'] : '';

    The above ternary assignment is equivalent to running the following logical expression for every single $_POST variable in which you’re interested:

    if( isset( $_POST['fname'] ) ) {
        $fname = $_POST['fname'];
    } else {
        $fname = '';
    }
    

    If you implement either of these, you won’t get a notice if you run empty() on $gender; furthermore, if $_POST['gender'] wasn’t set, empty() will still behave the way you expect. It adds a little verbosity, but to rewrite your example you might try:

    if( isset( $_POST["submit"] ) ) {
    
        $fname = isset( $_POST['fname'] ) ? $_POST["fname"] : '';
        $lname = isset( $_POST['lname'] ) ? $_POST["lname"] : '';
        $gender = isset( $_POST['gender'] ) ? $_POST["gender"] : '';
        $age = isset( $_POST['age'] ) ? $_POST["age"] : '';
        $address = isset( $_POST['address'] ) ? $_POST["address"] : '';
        $media = isset( $_POST['media'] ) ? $_POST['media'] : '';
    
        $errormessage =  array();
        if( empty( $fname ) )
            $errormessage[] = "Please enter your first name";
        if( empty( $lname ) )
            $errormessage[] = "Please enter your last name";
        if( empty( $gender ) )
            $errormessage[] = "Please select your gender";
        if( empty( $age ) )
            $errormessage[] = "Please select your age";
        if( empty( $address ) )
            $errormessage[] = "Please enter your address";
        if( empty( $media ) )
            $errormessage[] = "Please select the type of media";
    }
    

    Of course, you’d want to clean your data if you’re going to use it in a SQL context, but that should at least get you around the error you’re facing.

    If you had a lot of these variables–or were repeating this task often–you might consider rolling it up into a function!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started learning Erlang and am trying out some Project Euler problems
I have just started writing my own JavaScript Framework (just for the learning experience),
I've just started learning how to use pygame yesterday. I was read this one
I have just started learning C#. Can anyone explain the technical differences between a
I have just started learning C# and would have one question that I cannot
Hello i have just started learning mvc2 and im having a problem with the
I just started using CakePHP, and am learning how to use Bake. Why does
Have just started using Google Chrome , and noticed in parts of our site,
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have just started playing with ASP.NET MVC and have stumbled over the following situation.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.