Abdur Rehman Khalid
Abdur Rehman Khalid

Reputation: 33

PHP Form Validation and showing error message beside Input Fields

I am trying to Show an error message besides the input fields but I am not able to do so. I am not able to find what mistake I am making here. Below is the code of form and PHP. My code looks to me right but in browser I am not getting desired output as I am stuck on it. I would be thankful if some would help me.

    <?php

        $Name_Error = "";
        $Email_Error="";
        $Website_Error="";
        $Gender_Error="";

        function Test_User_Input($User_Data){
            return $User_Data;
        }

        if(isset($_POST['Submit'])){
            if(empty($_POST["Name"])){
                $Name_Error = "Kindly Enter the Name!";
            }
            else {
                $Name = Test_User_Input($_POST["Name"]);
            }
            if(empty($_POST["Email"])){
                $Email_Error = "Kindly Enter the Eamil Address!";
            }
            else {
                $Email = Test_User_Input($_POST["Email"]);
            }
            if(empty($_POST["Website"])){
                $Website_Error = "Kindly Enter the Website URL!";
            }
            else {
                $Website = Test_User_Input($_POST["Website"]);
            }
            if(empty($_POST["Gender"])) {
                $Gender_Error = "Kindly Select your Gender!";
            }
            else {
                $Gender = Test_User_Input($_POST["Gender"]);
            }
        }
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Simple Form</title>
</head>
<body>

    <form>
        <label>Enter your Name</label>
        <br>
        <input type="text" name="Name">*<?php echo $Name_Error ?>
        <br>
        <label>Enter your Email Address</label>
        <br>
        <input type="text" name="Email">*<?php echo $Email_Error ?>
        <br>
        <label>Enter your Website</label>
        <br>
        <input type="text" name="Website">*<?php echo $Website_Error ?>
        <br>
        <label>Select your Gender</label>
        <br>
        <input type="radio" name="Gender" value="Male"> Male
        <input type="radio" name="Gender" value="Female">Female *<?php echo $Gender_Error ?>
        <br>
        <label>Comments
        <br>
        <textarea name="Comment"></textarea>
        <br>
        <input type="Submit" name="Submit">
    </form>

</body>
</html>

Upvotes: 1

Views: 1240

Answers (3)

Switi
Switi

Reputation: 379

<?php


$Name_Error = $Email_Error = $Gender_Error = $Website_Error = "";
$Name = $Email = $Gender  = $Website = "";


    if(isset($_POST['Submit'])){
  if (empty($_POST["Name"])) {
    $Name_Error = "Name is required";
  } else {
    $Name = test_input($_POST["Name"]);
  }
  
  if (empty($_POST["Email"])) {
    $Email_Error = "Email is required";
  } else {
    $Email = test_input($_POST["Email"]);
  }
    
  if (empty($_POST["Website"])) {
    $Website_Error = "Kindly Enter the Website URL!";
  } else {
    $Website = test_input($_POST["Website"]);
  }

  if (empty($_POST["Gender"])) {
    $Gender_Error = "Gender is required";
  } else {
    $Gender = test_input($_POST["Gender"]);
  }
}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>

<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
  Name: <input type="text" name="Name">
  <span class="error">* <?php echo $Name_Error;?></span>
  <br><br>
  E-mail: <input type="text" name="Email">
  <span class="error">* <?php echo $Email_Error;?></span>
  <br><br>
  Website: <input type="text" name="Website">
  <span class="error"><?php echo $Website_Error;?></span>
  <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
  <input type="radio" name="Gender" value="other">Other
  <span class="error">* <?php echo $Gender_Error;?></span>
  <br><br>
  <input type="submit" name="Submit" value="Submit">  
</form>

<?php
echo "<h2>Your Input:</h2>";
echo $Name;
echo "<br>";
echo $Email;
echo "<br>";
echo $Website;
echo "<br>";
echo $Gender;
?>

</body>
</html>

Upvotes: 0

Gazi Md. Yeasin
Gazi Md. Yeasin

Reputation: 43

Add form action="" and method="POST". That will fix your problem.

Upvotes: 0

AraByte
AraByte

Reputation: 154

You need to call a specific page to trigger your PHP, in this case it's the page itself, the method is POST change <form> to <form action="" method="POST">

Upvotes: 1

Related Questions