首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >根据用户输入的关键字重定向

根据用户输入的关键字重定向
EN

Stack Overflow用户
提问于 2013-09-02 06:02:35
回答 3查看 582关注 0票数 0

我有一个页面,用户可以输入他正在销售的内容并被重定向到该页面。我知道mysql LIKE '%%' function,但我不使用数据库。如果情况符合,我只想重定向到那个页面。那件事怎么可能?是javascript还是php?

例如,,如果用户输入“膝上型计算机”,那么他/她将被重定向到laptop.php(我有页面)。我怎么能这么做?

表格结构:

代码语言:javascript
运行
复制
 <form class="sellcont" action="" method="post">
     <input type="text" name="selling" >
<input type="submit" name="submit" value="Next" class="myButton">
 </form>


 <?php if(isset($_POST['submit'])){
 $sell = $_POST['selling'];
 if(!empty($_POST['selling'])){

//This is where I am lost. 

}


 }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-09-02 06:06:13

后评论

您可以使用php header重定向输入($_POST‘good’)并检查输入($_POST‘good’)是否是一个好输入。

代码语言:javascript
运行
复制
$goodinput = array("laptop", "pc", "mac", "cellphone");

if(!empty($_POST['selling'])){

    if (in_array($_POST['selling'],$goodinput)){ //checks if user input is a  good input

       header('Location: http://www.yoururl.com/'.$_POST['selling'].'php');
       exit();

    } else {

       header('Location: http://www.yoururl.com/wedonthavethatpage.php');
       exit();

    }
}

建议:

  • 而不是创建单个页面,例如:laptop.php,而是将类似的值存储在数据库中并创建动态页面。
票数 2
EN

Stack Overflow用户

发布于 2013-09-02 06:08:53

PHP代码应该如下所示:

代码语言:javascript
运行
复制
<?php if(isset($_POST['submit'])){
 $sell = $_POST['selling'];
 if(!empty($_POST['selling'])){
     header('location: ./'.$_POST['selling'].'.php');   
 }
 else {
     header('location: ./index.php');  //index.php is PHP files on which user enter selling item
  }  
}
?>
票数 1
EN

Stack Overflow用户

发布于 2013-09-02 06:35:07

您也可以使用这个答案中的代码:

How can I check if a URL exists via PHP?

查看用户输入的内容是否可用。如果是,则执行重定向(如果不是),将其发送到另一个页面。

例如。

代码语言:javascript
运行
复制
if(!empty($_POST['selling'])){
    $file = 'http://www.yourdomain.com/'.$_POST['selling'].'.php';
    $file_headers = @get_headers($file);
    if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
        $exists = false;
    }
    else {
        $exists = true;
    }

    if ($exists) {
        header(sprintf('Location: %s', $file);
    }
    else {
        header('Location: http://www.yourdomain.com/other_page.php');
    }
    exit();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18566556

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档