提交表单后重定向到另一个页面


redirecting to another page after form submission

我在header.php(这是每个页面的标题)中有一个搜索表单。提交表单后,我想将用户重定向到页面:site.com/search-$VALUE-100

我试过:

$value = $_POST['value'];
if(isset($_POST['search']))
{
if(preg_match('/^[a-zA-Z0-9]$/', $value))
{
    header("Location: search-".$value."-100");
}
}

和 HTML:

<form method="get">
<input type="text" name="value" value="search" onfocus="if (this.value==this.defaultValue) this.value = ''" onblur="if (this.value=='') this.value = this.defaultValue" />
<input type="submit" name="search" class="image" value="" />
</form>

现在代码发送到:

page.php?value=a&search=
index.php?value=a&search=
user.php?value=a&search=
somepage.php?value=a&search=

您正在使用GET方法而不是POST,因此要访问此值,请使用$_GET

  $value = $_GET['value'];
  if(isset($_GET['search']))
  {
    if(preg_match('/^[a-zA-Z0-9]$/', $value))
    {
      header("Location:search-".$value."-100");
    }
  }
$value = $_GET['value'];
if(isset($_GET['search'])){
    if(preg_match('/^[a-zA-Z0-9]$/', $value)){
        header("Location: /search-".$value."-100");
    }
}

这应该做到

首先,您必须声明操作(又名.php脚本)将处理来自任何页面的请求,如果要在 $_POST 中具有值,则必须将方法声明为 POST:

<form action="search.php" method="POST">

比在搜索中.php做你的事情:

$value = $_POST['value'];
if(isset($_POST['search']))
{
  if(preg_match('/^[a-zA-Z0-9]$/', $value))
  {
    header("Location: search-".$value."-100");
  }
}
header('Location: http://'.$_SERVER['HTTP_HOST'].'/search-'.$value.'-100');