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"

<html>
<body>
<style type="text/css">

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

</body>
</html>

Here is my another file named "welcome.php"

<?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.

e.g.
$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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.