【发布时间】:2020-05-14 15:11:13
【问题描述】:
我正在通过编码和教程来学习 PHP,但是对我所学的内容进行建模以适应我的项目,我正在创建一个简单的页面,用户可以在其中提交将输出到 index.php 的文本。
目前,我的代码(来自 post.php)在加载时会自动重定向到 index.php。当我禁用代码的“检查错误”部分时,post.php 不会重定向。我的意图是在用户填写并提交表单后将页面重定向到 index.php。
我的代码“检查错误”部分有什么问题?
<?php
include('head.php');
include('header.php');
$userQuote = '';
$errorsPost = array('user_message'=>'');
if(isset($_POST['submit-post'])){
//check user quote
if(empty($_POST['user_message'])):
$errorsPost['user_message'] = 'You forgot to enter your message! Try posting again. <br />';
else:
$userQuote = $_POST['user_message'];
endif;
}
//end of post check
//check for errors
if(array_filter($errorsPost)):
// echo 'errors in the form';
else:
// echo 'form is valid';
header('Location: index.php');
endif;
?>
<div class="container-post" id="post">
<div class="container-post-form">
<form action="index.php" method="POST">
<textarea name="user_message" value="<?php echo h($userQuote) ?>" rows="5" cols="30" placeholder="Example: I don't want my cat Mazzy anymore. Does anybody want her? She likes to lick skin and it hurts." maxlength="560"></textarea>
<ul class="container-form-buttons">
<li>
<button type="submit" name="submit-post" value="submit-post" class="btn">Submit</button>
</li>
<li>
<button class="btn">Close</button>
</li>
</ul>
</form>
</div>
</div>
<div class="red-text header-error"><?php echo $errorsPost['user_message']; ?></div>
【问题讨论】:
-
您是否尝试过将错误检查器放入 后检查块的末尾?我认为只有在表单已经提交时才会发生重定向?
标签: php arrays redirect filter