如何根据写入输入字段的第一个字母重定向到页面(PHP)

时间:2014-02-06 21:44:23

标签: javascript php forms

我有这样的表格:

<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>

现在,当您以 a 作为第一个字母开头的单词时,我希望将用户重定向到 first.php ;如果你写一个以 b 开头的单词,你将被重定向到 second.php ;如果你写其他任何以任何字母开头但不是a或b ,你将被重定向到 third.php

我该怎么做?我如何在redirect.php中实现它?我的意思是,当您被重定向时,我也会收到您的姓名,实际上我在header('Location:函数后使用@mail,但我只重定向到一页。

3 个答案:

答案 0 :(得分: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 
}

看看它是否适合你..

答案 1 :(得分: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;

答案 2 :(得分: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);
相关问题