2014-02-06 44 views
-1

我有這樣的形式重定向到一個頁面:如何根據寫入輸入字段(PHP)的第一個字母

<form action="redirect.php" method="post" id="mainForm"> 
<input name="name" type="text" class="false" id="name" maxlength="10" /> 
<input type="submit" id="send" value="Send" /> 
</form> 

現在,當你寫的字開頭的一個的第一個字母,我希望用戶被重定向到first.php;如果你寫的字以b開始,你將被重定向到second.php;如果你寫其他任何,以任何字母開頭,但不是a或b您將被重定向到third.php

我該怎麼做?我該如何在redirect.php中實現它?我的意思是,當你將被重定向時,我也會收到你的名字,實際上我使用@mail函數後使用header('Location:,但我只重定向到一頁。

+0

@Thomas請原諒我的寫作和語法!我只是想舉個例子!無論如何感謝糾正。並感謝減號! – focusoft

+0

我不知道我必須知道英語語法!對不起! – focusoft

回答

1
if(isset($_POST['name'])){ 
    $name=trim($_POST["name"]); 
}else{ 
    // no name input.. REDIRECT back to the form? 
} 
if (strtolower(substr($name,0,1)) == 'a'){ 
    // REDIRECT To FIRST 
}elseif (strtolower(substr($name,0,1))=='b'){ 
    // REDIRECT TO seconde 
}else{ 
    // REDIRECT to third 
} 

看看它是否適合你..

+0

如果沒有名稱輸入會提醒用戶,並且您的代碼必須替換爲我的redirect.php文件中的標題功能?謝謝。 – focusoft

+1

我會假設你取代你想如何重定向頁面。而且沒有太多的驗證......即使你在前端有JS驗證,也需要進行後端驗證。 –

+0

非常感謝,你給我最好的解決方案。 – focusoft

1
<?php 
if(isset($_POST['name'] && strlen($_POST['name']) > 0) { 
    // send to first letter ($_POST['name'][0] 
    switch($_POST['name'][0]) { 
     case 'a': 
     case 'A': 
     // if the first letter is a or A 
     header('Location: http://www.example.com/first.php'); 
     break; 
     case 'b': 
     case 'B': 
     // otherwise if the first letter is b or B 
     header('Location: http://www.example.com/second.php'); 
     break; 
     default: 
     // if none of the above 
     header('Location: http://www.example.com/somePlace/third.php'); 
    } 
} else { 
    // if no name was supplied 
    header('Location: http://www.example.com/somePlace/noLetterSelected'); 
} 
exit; 
1
if(isset($_POST['name'])) 
    $input = $_POST['name']; 

function directMe($input) 
{ 
     $firstChar = strtolower(substr($input, 0, 1)); 
     if ($firstChar == 'a') 
      header('Location: ...first.php'); 
     elseif ($firstChar == 'b') 
      header('Location: ...second.php'); 
     else 
      header('Location: ...third.php'); 
} 

directMe($input); 
+0

感謝您的回答! – focusoft

相關問題