首页 > 解决方案 > 在收到字段后尝试将我的 PHP 表单重定向到另一个页面时出现错误

问题描述

当我在收到字段后尝试将我的 PHP 表单重定向到另一个页面时出现错误。

我创建了一个表单,并验证了输入。现在我想在单击提交按钮时将用户重定向到主页。

我首先检查输入是否有错误,如果没有错误,则应将用户重定向到主页。我不知道我做错了什么,但是在我点击提交按钮后我得到了这个错误。

错误信息:

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at postmaster@localhost to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

Apache/2.4.46 (Win64) OpenSSL/1.1.1g PHP/7.4.11 Server at 127.0.0.1 Port 80

这是我的代码:

<?php



$email = $title = $ingredients = '';

$error = [];


if($_SERVER['REQUEST_METHOD'] === 'POST'){



// check for email
if(empty($_POST['email'])){
  $error['email'] = ' Email is empty';
} else {
  $email =  $_POST['email'];
  if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
    $error['email'] = ' Email must contain @ and .';
  }
}

// check for title
if(empty($_POST['title'])){
  $error['title'] = ' Title is empty';
}  else {
  $title =  $_POST['title'];
  if(!preg_match('/^[a-zA-Z\s]+$/', $title)){
    $error['title'] = 'Title must be letters and spaces';
  }

}

// check for ingredients
if(empty($_POST['ingredients'])){
  $error['ingredients'] = ' Ingredients is empty';
}  else {
  $ingredients =  $_POST['ingredients'];
  if(!preg_match('/^([a-zA-Z\s]+)(,\s*[a-zA-Z\s]*)*$/', $ingredients)){
    $error['ingredients'] = 'Ingreients can only contain letters and comma seperated';
  }
}

if(array_filter($error)){

} else {
  die(header('Location : index.php'));
}

}



function xss_safe($value){
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
}

?>

<!DOCTYPE html>
<html lang="en">

  <?php include 'template/header.php'?>
  

  <form action="form.php" method="POST">

  <div class="input_div">
  <label >Email :</label>
  <input type="text" name="email" value="<?= xss_safe($email) ?>">
  <?= isset($error['email']) ? '<div class="error_msg">'.$error['email'].'</div>' : '' ?>
  
  <div class="input_div" >
  <label >Pizza Title :</label>
    <input type="text" name="title" value="<?= xss_safe($title) ?>" >
    <?= isset($error['title']) ? '<div class="error_msg">'.$error['title'].'</div>' : '' ?>
  
  <div class="input_div" >
  <label >Ingredients (comma seperated) :</label>
    <input type="text" name="ingredients" value="<?= xss_safe($ingredients) ?>">
    <?= isset($error['ingredients']) ? '<div class="error_msg">'.$error['ingredients'].'</div>' : '' ?>
  
  
  <div class="input_div" >
  <input type="submit" class="submitBtn" name="submit" value="Submit">
  </div>
  
  </form>


  <?= include 'template/footer.php' ?>

</html>

标签: phpformsredirect

解决方案


也许您尝试重定向到未退出的页面

<form action="form.php" method="POST">

检查您的文件名是否为form.php并从/form.php调用表单

查看截图:http: //joxi.net/8AnKGpqIyje8Vr


推荐阅读