我想在没有任何参数的情况下提交表单时重定向到具有表单的页面,我也想返回一条错误消息,我怎样才能从控制器重定向到表单?
< form action =controllers / Customer.controller.phpmethod =post> < label for =cellPhoneNo>手机号码< / label> < input type =textname =cellPhoneNoclass =textField/> < label for =telephone>电话号码< / label> < input type =textname =telephoneclass =textField/> < input type =submitname =searchCustomervalue =بحث/> < / form>以下是Customer.controller.php页面
if(trim($ _ POST ['cellPhoneNo'])==&& trim($ _ POST ['telephone'])==){ //include('Location:index.php'); //我应该在这里写什么? 回显空;解决方案
不知道你的结构框架,你可以使用PHP的头文件
if(trim($ _ POST ['cellPhoneNo'])==&& trim($ _ POST ['telephone'])==){ $ _SESSION ['error'] ='字段不能为空!'; 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.phpmethod =post>因此,无论何时提交表单,如果字段为空,表单页面将被重新加载,并且由于 $ b $ p>现在设置_SESSION错误,它将被显示。您可能想从$ _SESSION ['error']显示中创建一个函数,所以您不会在每个表单中编写所有代码。
编辑在评论之后: 嗯,我不太了解你的问题,你可以使用$ _GET:
header(Location:../index.php?page=customerSearch);,您可以通过 $ pageToInclude = $ _GET ['page']; //正确清理
或使用
$ _ SESSION ['pageToInclude'] ='CustomerSearch'; $ _SESSION ['error'] ='字段不能为空!'; header('Location:myformlocation.php'); ....以及您使用的索引
$ pageToInclude = isset($ _ SESSION ['pageToInclude'])? $ _SESSION ['pageToInclude']:'someotherdefaultpage';
I want to redirect to the page which has a form when user submit the form without any parameters, also I want to return an error message, how can I redirect from controller to the form?
<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>and here's the Customer.controller.php page
if(trim($_POST['cellPhoneNo']) == "" && trim($_POST['telephone']) ==""){ //include('Location:index.php'); //what I supposed to write here? echo "empty"; }解决方案
Not knowing the structure of your framework, you can use php's header
if(trim($_POST['cellPhoneNo']) == "" && trim($_POST['telephone']) ==""){ $_SESSION['error'] = 'Fields cannot be empty!'; header('Location: myformlocation.php'); exit(); }And just above your form:
<?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">So, whenever the form submits, if fields are empty, the form page is reloaded and, since $_SESSION error is now set, it will be displayed. You might want to make a function out of $_SESSION['error'] displaying, so you won't writing all that code in each form.
EDIT after comment: Uhm, I'm not really sure to understand your question, you can use either $_GET:
header("Location: ../index.php?page=customerSearch");and you retrieve it in index with $pageToInclude = $_GET['page']; //properly sanitized
or use
$_SESSION['pageToInclude'] = 'CustomerSearch'; $_SESSION['error'] = 'Fields cannot be empty!'; header('Location: myformlocation.php'); ....and in index you use
$pageToInclude = isset($_SESSION['pageToInclude']) ? $_SESSION['pageToInclude'] : 'someotherdefaultpage';
更多推荐
如果字段为空,则重定向到php页面
发布评论