我有一个页面,用户可以在其中输入他正在销售的产品,并被重定向到该页面。我知道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.
}
}
评论后
您可以使用phpheader
重定向并使用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();
}