我有一个页面,用户可以输入他正在销售的内容并被重定向到该页面。我知道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.
}
}
发布于 2013-09-01 22:06:13
后评论
您可以使用php header
重定向输入($_POST‘good’)并检查输入($_POST‘good’)是否是一个好输入。
$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
,而是将类似的值存储在数据库中并创建动态页面。发布于 2013-09-01 22:08:53
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
}
}
?>
发布于 2013-09-01 22:35:07
您也可以使用这个答案中的代码:
How can I check if a URL exists via PHP?
查看用户输入的内容是否可用。如果是,则执行重定向(如果不是),将其发送到另一个页面。
例如。
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();
}
https://stackoverflow.com/questions/18566556
复制相似问题