繁体   English   中英

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

[英]redirect based on the keyword the user entered

我有一个页面,用户可以在其中输入他所销售的商品并重定向到该页面。 我知道mysql LIKE '%%' function但是我没有使用数据库。 如果大小写匹配,我只想重定向到该页面。 那怎么可能? 无论是JavaScript还是PHP?

例如:如果用户输入“笔记本电脑”,那么他/她将被重定向到laptop.php(我有页面)。 我怎样才能做到这一点?

表格结构:

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

}


 }

评论后

您可以使用php header进行重定向,并使用in_array检查输入($ _POST ['selling'])是否正确。

$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而是将类似的值存储在数据库中并创建动态页面。

PHP代码应如下所示:

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

您还可以在以下答案中使用代码:

如何通过PHP检查URL是否存在?

查看用户输入的内容是否可用。 如果是这样,请执行重定向,否则请将其发送到备用页面。

例如

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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM