Display form validation error message on same page using only PHP?

Bricriu

I'm very new to PHP and I've cobbled this together from some other answers on here. Can anyone show me how to get the $errMsg to display? At present, a blank or incorrect name leads to a blank page. Is this because the form isn't being displayed again? If so, how should I go about 'reloading' the form with the error message?

<?php
$name = "Fred";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

  if (!empty($_POST["name"])) {

      if ($_POST["name"] == $name) {
        include("welcomeFred.php");
      }

      else {
        $errMsg = "Incorrect name";
      }

  }

  else {
    $errMsg = "Name required";
  }

}
else { ?>

  <html>
  ...
  <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    <input type="text" name="name" required>
    <span><?php echo $errMsg;?></span>
    <input type="submit" value="Submit">
  </form>
  ...
  </html>

<?php } ?>
Jerodev

You shouldn't put the rendering of the form in the else of your if structure. This is the reason your form isn't loaded when you submit the form.

Remove the else { ?> and <?php } ?> at the end of your file and it should work fine.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Form validation display same error message for all fields

From Dev

How do I display an error message on the same page in php

From Dev

How to display login error message on the same page in a php based system?

From Dev

Printing validation message on the same page in PHP

From Dev

Display Message only upon validation using jQuery

From Dev

form action is another page(php code) how to validate the form and display error message in view page

From Dev

Send post form to the same page using php or html only

From Dev

Display recaptcha error message on the same page

From Dev

Php mail form successfull message on same page

From Dev

Php mail form successfull message on same page

From Dev

Form validation with error message using angular ngMessage

From Dev

Form and PHP result display on same page

From Dev

Unable to show django form Validation error on same page

From Dev

PHP form is not capturing message due to unkown validation error

From Dev

How to display Meteor.loginWithPassword callbak error message on the same page

From Dev

Sitecore|WFFM| Custom Error Message with details| on Same Page with Form

From Dev

error message in jquery form validation

From Dev

Form validation - Error Message not output

From Dev

Form to run PHP script and display output on same page

From Dev

How can I display the error message after submitting the form in php?

From Dev

how to display php error message on web page in firefox?

From Dev

Display validation message on submitting the form in Angularjs

From Dev

Only The Validation Message Is Returning, Not The Error Message

From Dev

Display a form and the output on the same page

From Dev

display error message on a login form

From Dev

PHP Validation error message order

From Dev

PHP externel file contact form that display a message after redirect to the form page

From Dev

display validation error message next to the textbox in codeigniter

From Dev

display validation error message next to the textbox in codeigniter

Related Related

  1. 1

    Form validation display same error message for all fields

  2. 2

    How do I display an error message on the same page in php

  3. 3

    How to display login error message on the same page in a php based system?

  4. 4

    Printing validation message on the same page in PHP

  5. 5

    Display Message only upon validation using jQuery

  6. 6

    form action is another page(php code) how to validate the form and display error message in view page

  7. 7

    Send post form to the same page using php or html only

  8. 8

    Display recaptcha error message on the same page

  9. 9

    Php mail form successfull message on same page

  10. 10

    Php mail form successfull message on same page

  11. 11

    Form validation with error message using angular ngMessage

  12. 12

    Form and PHP result display on same page

  13. 13

    Unable to show django form Validation error on same page

  14. 14

    PHP form is not capturing message due to unkown validation error

  15. 15

    How to display Meteor.loginWithPassword callbak error message on the same page

  16. 16

    Sitecore|WFFM| Custom Error Message with details| on Same Page with Form

  17. 17

    error message in jquery form validation

  18. 18

    Form validation - Error Message not output

  19. 19

    Form to run PHP script and display output on same page

  20. 20

    How can I display the error message after submitting the form in php?

  21. 21

    how to display php error message on web page in firefox?

  22. 22

    Display validation message on submitting the form in Angularjs

  23. 23

    Only The Validation Message Is Returning, Not The Error Message

  24. 24

    Display a form and the output on the same page

  25. 25

    display error message on a login form

  26. 26

    PHP Validation error message order

  27. 27

    PHP externel file contact form that display a message after redirect to the form page

  28. 28

    display validation error message next to the textbox in codeigniter

  29. 29

    display validation error message next to the textbox in codeigniter

HotTag

Archive