我想,当用户提交表单,不带任何参数,也是我想返回的错误信息重定向到具有形式的页面,我如何从控制器重定向到形式?
<form action="controllers/Customer.controller.php" method="post">
<label for="cellPhoneNo">cell phone number</label>
<input type="text" name="cellPhoneNo" class="textField"/>
<label for="telephone">telephone number</label>
<input type="text" name="telephone" class="textField"/>
<input type="submit" name="searchCustomer" value="بحث"/>
</form>
和这里的Customer.controller.php页
if(trim($_POST['cellPhoneNo']) == "" && trim($_POST['telephone']) ==""){
//include('Location:index.php'); //what I supposed to write here?
echo "empty";
}
不知道你的框架结构,你可以使用PHP的头
if(trim($_POST['cellPhoneNo']) == "" && trim($_POST['telephone']) ==""){
$_SESSION['error'] = 'Fields cannot be empty!';
header('Location: myformlocation.php');
exit();
}
而就在你上面的表格:
<?php if(isset($_SESSION['error'] )) : ?>
<div class="error"><?php echo $_SESSION['error'];?></div>
<?php
unset($_SESSION['error']);
endif; ?>
<form action="controllers/Customer.controller.php" method="post">
所以,当表单提交,如果字段为空,表单页面重新加载,并且由于$ _SESSION错误现在设置,它就会显示出来。 你可能想使一个功能出$ _SESSION [“错误”]显示,这样你就不会在每个窗体编写所有的代码。
评论后编辑:
嗯,我真的不知道要明白你的问题,你可以使用$ _GET:
header("Location: ../index.php?page=customerSearch");
你在索引检索$pageToInclude = $_GET['page'];
//正确消毒
或使用
$_SESSION['pageToInclude'] = 'CustomerSearch';
$_SESSION['error'] = 'Fields cannot be empty!';
header('Location: myformlocation.php');
....
在指数使用
$pageToInclude = isset($_SESSION['pageToInclude']) ? $_SESSION['pageToInclude'] : 'someotherdefaultpage';
<?php
session_start();
if(isset($_POST)){
$cont=true;
//cellPhoneNo
if(!isset($_POST['cellPhoneNo']) || strlen($_POST['cellPhoneNo'])< 13){ //13 being the telephone count
$cont=false;
$_SESSION['error']['cellPhoneNo']='Cell phone is required & must be 13 in length';
header('Location: ./index.php');
die();
}
//telephone
if(!isset($_POST['telephone']) || strlen($_POST['telephone'])< 13){ //13 being the telephone count
$cont=false;
$_SESSION['error']['telephone']='Telephone is required & must be 13 in length';
header('Location: ./index.php');
die();
}
if($cont===true){
//continue to submit user form
}else{
header('Location: ./index.php');
die();
}
}else{
header('Location: ./index.php');
}
?>