PHP: Refresh the page in an invalid form send

How to refresh the page with the form when sending, waiting for the results of the submitted data and display the result.

For example, I have a page with the form:

<form action="" method="post">
   <input type="name" value="" name="name" placeholder="Your Name" />
   <input type="button" name="submit" value="submit form "/>
</form>

The mechanism that processes the form is external, but is required on the page:

require_once 'form_engine.php';

form_engine.php validates input,

$success = "true";
$errorMessage = " ";
$name = $_POST['name'];

if ( $name == '') {
      $errorMessage = 'Please enter your name';
      $success = false;
}
else (if $success = true) {
   // do something with the data

}

The form page contains the result:

<form action="" method="post">
   <input type="name" value="" name="name" placeholder="Your Name" />
   <input type="button" name="submit" value="submit form "/>
</form>
<p><?php echo $errorMessage; ?></p>

Will an error message be displayed after the form is submitted incorrectly? Or do I need to use a session to store it?

+4
source share
4 answers

You need something like this:

if (!isset($_POST['name']))

instead

if ( $name == 'name')

UPDATE

Try this, this should give you an idea:

<?php

    $errorMessage = false;

    if (isset($_POST['submit'])) {

        if (!isset($_POST['name']) || $_POST['name']=='') {
            $errorMessage = 'Please enter your name';
        }
        else {
           // do something with the data
           echo "Success!!";
        }
    }
?>

<form method="post">
   <input type="name" value="" name="name" placeholder="Your Name" />
   <input type="submit" name="submit" />
</form>
<p><?php if ($errorMessage) echo $errorMessage; ?></p>

Note: if you leave the attribute action, it simply sends the form to the current page

2: PHP . require() - , .

+4

, ! header().

$success = true;
$errorMessage = " ";
$name = $_POST['name'];

if(isset($_POST['name'])) {
 if ( $_POST['name'] == '') {
      $errorMessage = 'Please enter your name';
      $success = false;
      header('Location: www.something.com/some.php');
 }
 else if ($success == true) {
   // do something with the data
}

}
+1

php:

header('Location: www.mysite.com/index.php');
+1

, , , . . - , , , . , " " , /.


, :

a chart showing a typical flow of request and then render for a form

, , URI, ; , , , . , , , , — , — PHP.

, , , - , HTTPS/SSL. / , . , htmlentities, , . HTML- ( ;), ENT_QUOTES.


, , , , PHP , , . - , , . , :

<?php

require_once 'form_engine.php';

$name = !empty($_POST['name']) ? trim($_POST['name']) : '';
$name = htmlentities($name);

if ( $success ) {
  header('location: next-step.php');
  exit;
}

?>
<form action="" method="post">
  <input type="name" value="<?php echo $name; ?>" name="name" placeholder="Your Name" />
  <input type="button" name="submit" value="submit form "/>
</form>
<?php

if ( $errorMessage ) {
  echo "<p>$errorMessage</p>";
}

?>


form_engine.php , , , engine — .

+1

Source: https://habr.com/ru/post/1539527/


All Articles