我有(简化),这样的形式:
<form method="post" action="file.php">
<input name="test">
<button>Send</button>
</form>
<?php
session_start();
if(isset($_SESSION['error'])){
echo '<p>'.$_SESSION['error'].'</p>';
unset($_SESSION['error']);
}
?>
并在 file.php :
session_start();
$val = $_POST['test'];
if(!isset($val) || strlen($val) == 0 || empty($val)){
$_SESSION['error'] = 'The "test" field cannot be empty."
}
header("Location: {$_SERVER['HTTP_REFERER']}");
但是,当我单击带有空字段的发送时,错误会显示但表单已清除。有办法解决这个问题吗?