如何在验证失败后以及使用PHP重定向页面时保留表单数据


How to keep form data after validation fail and when the page redirects with PHP?

我有一个在教程外使用的表单。当出现验证错误时,我会尝试填充字段。

这是我的表格:

<div class="add">
<?php $errors4 = errors_seesion_funtion(); ?>
<?php echo form_errors($errors4); ?>
<div class="error-message"><?php echo message(); ?></div>
    <div class="done"><a href="../manage-employee-qsps.php"><input name="Done" type="button" value="Done" /></a></div>
    <h2>ADD New Department:</h2>
<form action="create-department-process.php" method="post">
      <p class="department-name">Department name:
        <input type="text" name="department_name" id="department-name" value="<?php if (isset($_POST['department_name'])) { echo htmlentities($_POST['department_name']); } ?>" />
      <span class="error">* <?php if (!empty($errors4)) { echo "<div class='"error'">";
          echo "Hi";
          echo "</div>";
      }
      ?></span>
      </p>
      <p class="department-name">Test name:
        <input type="text" name="test_name" id="test-name" value="" />
      <span class="error">* <?php /*echo form_errors($errors4); */
      if (!empty($errors4)) {
          echo "<div class='"error'">";
          echo "test name";
          echo "</div>";
      }
      ?></span>
      </p>
      <input type="submit" name="dept_added" id="add-btn" value="ADD Department" />
    </form>
<br />
<div class="cancel"><a href="../manage-employee-qsps.php">Cancel</a></div>

这是我的课程:

session_start();
function message() {
    if (isset($_SESSION["message"])) {
        $output = "<div class='message'>";
        $output .= htmlentities($_SESSION["message"]);
        $output .= "</div>";
        // clear message after use
        $_SESSION["message"] = null;
        return $output;
    }
}
function errors_seesion_funtion() {
    if (isset($_SESSION["errors3"])) {
        $errors2 = $_SESSION["errors3"];
        $_SESSION['post_data'] = $_POST;
        // clear message after use
        $_SESSION["errors3"] = null;
        return $errors2;
    }
}

这是我的验证功能:

$errors_array = array();
function fieldname_as_text($fieldname) {
  $fieldname = str_replace("_", " ", $fieldname);
  $fieldname = ucfirst($fieldname);
  return $fieldname;
}
function has_presence($value) {
    return isset($value) && $value !== "";
}
function validate_presences($required_fields) {
  global $errors6;
  foreach($required_fields as $field) {
    $value = trim($_POST[$field]);
    if (!has_presence($value)) {
        $errors6[$field] = fieldname_as_text($field) . " can't be blank";
    }
  }
}

这是我的create-department-process.php

if (isset($_POST['dept_added'])) {
$department_name = mysql_prep($_POST["department_name"]);
//Validations for form
$required_fields = array("department_name", "test_name");
validate_presences($required_fields);
if (!empty($errors6)) {
    $_SESSION["errors3"] = $errors6;
    redirect_to("add-department.php"); //this is the page the form is on
}
// Process the form
$query1  = "INSERT INTO departments (";
$query1 .= "  department_name ";
$query1 .= ") VALUES ( ";
$query1 .= "  '{$department_name}' ";
$query1 .= ") ";
$result1 = mysqli_query($db_connection, $query1);
if ($result1) {
    // Success
    $_SESSION["message"] = "Department created.";   
    redirect_to("add-department.php");
} else {
    // Failure
    $_SESSION["message"] = "Department creation failed.";
    redirect_to("creation-error.php");
}
} else {
redirect_to("fail.php");
}

我试着把这个放在我的表格的价值中

<?php if (isset($_POST['department_name'])) { echo      htmlentities($_POST['department_name']); } ?>

但是,当PHP运行表单验证和重定向时,我输入的值不会保留下来。当我出现验证错误时,有人知道如何将我键入的数据保留在表单字段中吗?

感谢您的时间和帮助!我真的很感激!

我认为当你这样做时,你的POST数据会丢失:

if (!empty($errors6)) {
    $_SESSION["errors3"] = $errors6;
    redirect_to("add-department.php"); //this is the page the form is on
}

我猜redirect_to实际上会将浏览器重定向到指定的页面,从而重置REQUEST值并丢失以前的POST数据。您需要将POST值保存在会话(la errors_seesion_funtion)中,并在表单中从那里访问它们,或者在上面的表单中保存include以保留原始POST值。