0

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

j08691
  • 204,283
  • 31
  • 260
  • 272
Sujan
  • 7
  • 1
  • 6
  • Javascript validation on the page itself, PHP validation after you submit. Verify that all fields are filled out. – aynber Nov 09 '16 at 18:05
  • Is there any mistake in my code? Why I am seeing this error only for radio button? – Sujan Nov 09 '16 at 18:39
  • Radio buttons and checkboxes don't exist until they have a selected value. – aynber Nov 09 '16 at 19:03
  • Then why this code doesn't show error when I copying this from here http://www.w3schools.com/php/showphp.asp?filename=demo_form_validation_escapechar – Sujan Nov 09 '16 at 19:06
  • Some versions of PHP can suppress that error, or don't register it at all. They probably have error messages turned off. – aynber Nov 09 '16 at 19:12
  • thanks anyber for your help – Sujan Nov 09 '16 at 19:27

0 Answers0