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

时间:2015-03-18 16:42:06

标签: php forms validation

我有这个表格,我正在使用一个教程。我正在尝试在出现验证错误时保持填充字段。

这是我的表格:

<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运行表单验证并重定向时,我输入的值不会保留。当我遇到验证错误时,有没有人知道如何将我输入的数据保存到表单字段中?

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

1 个答案:

答案 0 :(得分:0)

我认为当您执行此操作时,您的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值。