Lukas Lukas - 5 months ago 6
HTML Question

PHP Errors aren't showing

I have a simple

HTML script
:

<form action="" method="post">
<input type="text" name="amount">
<?php echo $amountError; ?>
<input type="submit">
</form>


And I display errors using this:

<?
$amount = $_POST['amount'];

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (empty($_POST['amount'])) {
$amountError = 'Empty amount';
} else {
header("Location: apmoketi.php");
}
?>


In the
apmoketi.php
file there is only one line
echo $amount = $_POST['amount'];


However, when I'm trying to display data in the
apmoketi.php
page, I don't get the value of the
$amount
variable using
action=""
, but using
action="apmoketi.php"
I can get it but my
PHP
code doesn't checking for any errors, why?

Answer

A call to header("Location: apmoketi.php"); makes the browser follow the specified URL as another, completely separate request. This new request doesn't have access to the POST data of the previous one.

Your options:

  1. use include so apmoketi.php has access to the variables of the current script
  2. use sessions to preserve data between requests
  3. use a query parameter to carry the value over:

    header('Location: apmoketi.php?amount=' . urlencode($amount));