1

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>
1
  • Have you tried making the input max width to something like 50%? Commented Jul 1, 2018 at 14:27

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Worth noting that action attribute isn't needed if it is the page itself
That's right, i pointed that so he will not forget to specify the action page and method for his next forms
0

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

Comments

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

3 Comments

Code dumps with no explanation are discouraged on SO. It's not a free coding service. Upvotes for quality answers are accrued over time, as future visitors learn something that they can apply to their own coding issues. Consider editing your post to highlight important bits, and explaining how/why they address the OP's issue. You can also include links to documentation, if that would help people follow up on tricky implementations.
In the following code we have added some new variables: $Name_Error, $Email_Error , $Gender_Error , and $Website_Error . These error variables will hold error messages for the required fields. We have also added an if else statement for each $_POST variable. This checks if the $_POST variable is empty (with the PHP empty() function). If it is empty, an error message is stored in the different error variables, and if it is not empty, it sends the user input data through the test_input() function:
Then in the HTML form, we add a little script after each required field, which generates the correct error message if needed (that is if the user tries to submit the form without filling out the required fields):

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.