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";
}
<?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');
}
?>
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';
精彩评论