I am learning php from w3school and I am trying to run this code but there is shown an error when I click submit button without filling the form. Here goes my code which is in a file named "form.php"

<style type="text/css">

<h2>PHP Form Validation Example</h2>
<form method="post" action="welcome.php">  
  Name: <input type="text" name="name">
  E-mail: <input type="text" name="email">
  Website: <input type="text" name="website">
  Comment: <textarea name="comment" rows="5" cols="40"></textarea>
  <input type="radio" name="gender" value="female">Female
  <input type="radio" name="gender" value="male">Male
  <input type="submit" name="submit" value="Submit">  


Here is my another file named "welcome.php"


  $name = $_POST["name"];
  $email = $_POST["email"];
  $website = $_POST["website"];
  $comment = $_POST["comment"];
  $gender = $_POST["gender"];
    echo "<h2>Your Input:</h2>";
    echo $name;
    echo "<br>";
    echo $email;
    echo "<br>";
    echo $website;
    echo "<br>";
    echo $comment;
    echo "<br>";
    echo $gender;

But when I submit form without filling the form its shown this.

Notice: Undefined index: gender in C:\xampp\htdocs\self\welcome.php on line 7

Please someone help to find the error............

Because the value of radio button is not set (index), the PHP throws an exception. You may need to check whether the value is set first using isset(). That way, you would be able to properly set a default value or display error message.

$gender = "Unknown";
if (isset($_POST["gender"]) { $gender = $_POST["gender"]; }

shorter version using ternary operator:
$gender = isset($_POST["gender"]) ? $_POST["gender"] : "Unknown";
Be a part of the DaniWeb community

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