2011-08-07 51 views
0

我想重定向到具有表單的頁面,當用戶提交表單時沒有任何參數,我也想返回一個錯誤信息,我該如何從控制器重定向到表單?重定向到php頁面如果字段爲空

<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"; 
} 

回答

1

不知道你的框架結構,你可以使用PHP的header

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 ['error']顯示中創建一個函數,因此您不會在每個表單中編寫所有代碼。評論後

編輯:
嗯,我真的不知道要明白你的問題,你可以使用$ _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'; 
+0

我怎麼能走一步回到了文件夾,因爲index.php的是外面的控制器文件夾? – palAlaa

+0

@Alaa'../ index.php'應該可以做到。你可以提供一個完整的路徑,'http:// mysite.com/index.php' –

+0

@ Damien Pirsy,如何將一個參數傳遞給index.php,因爲我這樣做了include(「subpages/$ pageToInclude.sub .PHP「);在索引中,我使$ pageToInclude =「CustomerSearch」befor header('Location:../index.php');但id不起作用! – palAlaa

2
<?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'); 
} 
?> 
+0

我不會在電話號碼中檢查is_int,因爲通常值爲+39 02 ..或347/123456或任何可接受的電話號碼值。電話號碼不是整數 –

+0

哦是啊忘了+的 –

+0

@勞倫斯Cherone,這是什麼警告「警告:不能修改標題信息 - 已經發送的頭文件(輸出開始於C:\ AppServ \ www \ crm \ controllers \ Customer.controller.php:5)在第11行的C:\ AppServ \ www \ crm \ controllers \ Customer.controller.php「 – palAlaa