divinity02 6 Posting Whiz in Training

I am trying to validate my registration form but my php codes blocking it some how, I keep this error, Notice: undefined index: in gender c and for the life of me cant seem to find the correct way to fix it nothing I tried is working.

and also I have been trying to insert data in the database and it is not inserting at all, I knew there is an error somewhere within the codes and I cant find. can someone plzzzzzzzzzzz help me fix this. here is both the php scripts and the html to go with it

this is the html codes

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title> Register </title>
        <link rel="stylesheet" type="text/css" href="../css/stylesheet.css" />
    </head>
    <body>
        <div id="container" >
            <div id="header">
                <img src="../images/header.jpg" alt="header" />
            </div>
            <div id="navbar">
                <ul>
                    <li><a href="../index.html" id="tlinks" class="toplinks" >Homepage</a></li>
                    <li><a href="featureNationalWonders.html" id="thold" class="toplinks">Feature National Wonders</a></li>
                    <li><a href="contact.html" id="linkhold" class="toplinks">Contact Us</a></li>
                    <li><a href="register.html" id="phold" class="toplinks">Sign Up</a></li>
                    <li><a href="login_user.html" id="loglinks" class="toplinks">Login</a></li>
                    <li><a href="aboutus.html" id="panlinks" class="toplinks">About Us</a></li>

                </ul>   
                    <form method="post" id="search"   action="" >
                        <div>
                            <input type="text" name="search" value=""/>
                            <input type="submit" value="Search"/>
                        </div>                      
                    </form> 
            </div>
            <br/><br/>
            <div id="contentArea">
                <form method="post" action="../php/register_user1.php" name="frmRegister" onsubmit="return validateForm()";>

                        <p>
                            <label> User Personal Information</label>
                        </p>
                                <p>
                                     <label>Title</label>
                                    <select id="title" name="title">
                                        <option value="" selected="selected" > Select a Title</option>
                                        <option value="Ms">Ms.</option>
                                        <option value="Mrs">Mrs.</option>
                                        <option value="Mr">Mr.</option>
                                        <option value="Dr">Dr.</option>
                                    </select>
                                </p>

                            <p> 
                                 <label>First Name:</label>
                                <input type="text" name="firstname" id="firstname" size="47"/>

                            </p>
                            <p>
                                <label>Last Name:</label>
                                <input type="text" name="lastname" id="lastname" size="47"/>
                            </p>
                            <p>
                                <label> UserName:</label>
                                <input type="text" name="username" id="username"  size="44"/>
                            </p>
                            <p>
                                <label> Email Address:</label>                      
                                <input type="text" id="email" name="email" size="43"/>
                            </p>
                            <p>
                                <label>Gender:</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                <input type="radio" name="gender" id="gender" value="male"/>Male 
                                <input type="radio" name="gender" id="gender" value="Female"/>Female
                            </p>
                            <p>
                                <label> Services:</label>
                                Magical Mermaid Hike: <input type="checkbox" name="services"/> 
                                Kayaking:<input type="checkbox" id="services" name="services"/>
                                Turtle Watching:<input type="checkbox" id="services"name="services"/>
                                Bus Tours:<input type="checkbox" id="services"name="services"/>
                                Sightseeing:<input type="checkbox" id="services" name="services"/>
                            </p>
                            <p>
                                <label>Address:</label>
                                <textarea name="address" id="address" cols="46" rows="9"></textarea>
                            </p>
                            <p>
                                <label> Password: </label>
                                <input type="password" id="mypwd" name="mypwd" size="47"/>
                            </p>
                            <p>
                                 <label>Confirm Password:</label>
                                <input type="password" name="confmypwd" id="confmywd" size="39"/>
                            </p>

                                <input type="submit" name="enter" value="Register" class="btnEnter"/>
                                <input type="reset" name="reset" value="RESET"/>

                </form>

                <p>
                    <a href="http://validator.w3.org/check?uri=referer"><img
                      src="http://www.w3.org/Icons/valid-xhtml11" alt="Valid XHTML 1.1" height="31" width="88" /></a>
                </p>
                <p>
                <a href="http://jigsaw.w3.org/css-validator/check/referer">
                    <img style="border:0;width:88px;height:31px"
                    src="http://jigsaw.w3.org/css-validator/images/vcss-blue"
                    alt="Valid CSS!" />
                </a>
            </p>

            </div>
                <div id="footerFix"></div>
                <div id="footerbox">
                    <div id="footer" >
                        <div id="footerholder">
                        <div>
                            <ul>
                                <li><a href="../index.html" class="toplinks" >Homepage</a></li>
                                <li><a href="featureNationalWonders.html"  class="toplinks">Feature National Wonders</a></li>
                                <li><a href="contact.html" class="toplinks">Contact Us</a></li>
                                <li><a href="register_user.html" class="toplinks">Sign Up</a></li>
                                <li><a href="login_user.html" class="toplinks">Login</a></li>
                                <li><a href="aboutus.html" class="toplinks">About Us</a></li>
                            </ul>
                        </div>
                        </div>
                    </div>            
                </div>

        </div>

    </body>
</html>

here is the php codes and

<?php

        //$register=$_POST['register_user']; 
        if(isset($_POST['Register']))
        {

            //capture the variable from the form and store in php variables

            $title=$_POST['title']; 
            $firstname=$_POST['firstname'];
            $lastname=$_POST['lastname'];
            $username=$_POST['username'];
            $email=$_POST['email'];
            $gender=$_POST['gender'];       <------- this is where the error is. gender 
            $address=$_POST['address'];         
            $mypwd=$_POST['mypwd'];

            include'db_server.php';

        $sql="SELECT * FROM members WHERE  username='$username'";

        $result=mysqli_query($conn,$sql) or die("Error:" .mysqli_error());

        $rowcount=mysqli_num_rows($result);

        if($rowcount>=1)
        {
            echo"<script type=\"text/javascript\">
            alert('Welcome!! Firstname Lastname, you are now a member of the Caribbean Nature Seekers Institute TT(CNSITT)');
            window.location=\"../xhtml/login_user.html\";
            </script>";

        }

        else
        {

            $sql="INSERT INTO members 
            VALUES('$title', '$firstname','$lastname', '$username', '$email', '$gender', '$address', md5('$mypwd'))";
            if(mysqli_query($conn,$sql))
            {
                session_start();
                $_SESSION['user']=$username;
                mysqli_close($conn);
                header("location:login_user.html");

            }

            else
            {

                echo"Error insert values into database";
            }

        }

    }

?>

Dani AI

Generated

— the PHP notice for "undefined index: gender" means $_POST['gender'] does not exist when the script reads it. That usually happens for one of two reasons: the radio button was not selected by the user, or the server-side check that runs your code is mismatched so the script runs at times you did not expect. Also there are a few other fragile spots in the thread code that will prevent insertion or hide useful errors (missing column list in INSERT, use of MD5, mysqli_error() called without the connection, and duplicate element ids).

Quick, practical fixes:

  • Use a request-method check (or match the submit input name exactly) instead of relying on a non-existent POST key.
  • Read optional fields defensively so PHP cannot emit a notice.
  • Use prepared statements and include column names in your INSERT.
  • Replace MD5 with password_hash().
  • If you want multiple checkboxes, name them as an array (e.g. name="services[]").

Example of a safe pattern (adapt to your connection variable and PHP version):

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $gender = $_POST['gender'] ?? null;          // avoids the notice
    $username = trim($_POST['username'] ?? '');
    $password = $_POST['mypwd'] ?? '';

    if ($username === '' || $password === '') { /* handle missing required fields */ }

    $hash = password_hash($password, PASSWORD_DEFAULT);

    $stmt = $conn->prepare(
        "INSERT INTO members (title, firstname, lastname, username, email, gender, address, password) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
    );
    $stmt->bind_param('ssssssss', $_POST['title'], $_POST['firstname'], $_POST['lastname'],
                      $username, $_POST['email'], $gender, $_POST['address'], $hash);
    if (! $stmt->execute()) { error_log('DB error: '.$stmt->error); }
    else { session_start(); $_SESSION['user'] = $username; header('Location: login_user.html'); exit; }
}

Short checklist to debug now:

  • Match the PHP submit-check to the input name or use $_SERVER['REQUEST_METHOD'].
  • Change checkbox names to services[] if you expect multiple values.
  • Use mysqli_error($conn) or $stmt->error for meaningful DB messages.
  • Temporarily enable full error reporting while debugging: error_reporting(E_ALL); ini_set('display_errors',1);

These changes will remove the notice, make the insert reliable, and avoid security problems such as SQL injection and weak password storage.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.