【问题标题】:Redirect after form submitted php表单提交后重定向php
【发布时间】:2013-09-10 20:13:04
【问题描述】:

我有以下正确发布的代码,但我想重定向到站点 index.php 页面并且无法弄清楚。我到处搜索并尝试了几乎所有方法,但都没有成功。任何帮助表示赞赏。

$editFormAction = $_SERVER['PHP_SELF'];

    $query_dupUserCheck = "SELECT tblUser.userKey FROM tblUser WHERE tblUser.username = '".$_POST['username']."'";
    $sqlsearch = mysql_query($query_dupUserCheck);
    $resultcount = mysql_numrows($sqlsearch);

    $query_dupUserCheck2 = "SELECT tblUser.userKey FROM tblUser WHERE tblUser.email = '".$_POST['user_email']."'";
    $sqlsearch2 = mysql_query($query_dupUserCheck2);
    $resultcount2 = mysql_numrows($sqlsearch2);

    if ($resultcount > 0 & $resultcount2 > 0) { 
        print("That Username and Email Already Exists");
    } else {
        if ($resultcount > 0) { 
            print("That Username Already Exists");  
        } else {
            if ($resultcount2 > 0) {    
            print("That Email Already Exists");
            } else {

if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "createAccount")) {
  $insertSQL = sprintf("INSERT INTO tblUser (username, password, userTypeKey, email) VALUES (%s, %s, %s, %s)",
                       GetSQLValueString($_POST['username'], "text"),
                       GetSQLValueString($_POST['user_password2'], "text"),
                       GetSQLValueString($_POST['userType'], "int"),
                       GetSQLValueString($_POST['user_email'], "text"));

  mysql_select_db($database_ignite, $ignite);
  $Result1 = mysql_query($insertSQL, $ignite) or die(mysql_error());

  $insertGoTo = "index.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
header(sprintf("Location : %s", $insertGoTo));
}
}}}

表单用

调用动作
<?php echo $editFormAction;?>

【问题讨论】:

  • 因为header(sprintf("Location : %s", $insertGoTo));Location: 之间的空间尝试header(sprintf("Location: %s", $insertGoTo)); 仅此一项就会失败。现在就试试吧。
  • 你可以使用 javascript 来做到这一点

标签: php forms redirect


【解决方案1】:

As per my original comment: (link)

因为Location和分号:之间有空格

header(sprintf("Location : %s", $insertGoTo));
// ---------------------^

试试:

header(sprintf("Location: %s", $insertGoTo));

仅此一项就会失败。现在就试试吧。

请参阅header() 函数的 PHP 手册。 http://php.net/manual/en/function.header.php

【讨论】:

  • @user2752153 还有其他问题,我无法使用您的代码进行测试,但Location 和分号之间的空格绝对是一个因素。我会看看我还能做些什么来测试你的一些代码。
  • @user2752153 可能和$insertGoTo = "index.php";有关?
  • 我尝试删除它,然后放入 header("Location:index.php");但仍然没有运气
  • @user2752153 因为这个$editFormAction = $_SERVER['PHP_SELF'];,它停留在同一页面上,您需要将您的操作设置到另一个页面。
  • @user2752153 这个header("Location:index.php"); 不起作用,你需要在Location: 之间留一个空格,就像这样header("Location: index.php"); 非常重要。
【解决方案2】:

您的问题在于对标头的调用;删除 Location 和 : 之间的空格

header("Location: $insertGoTo");

OR 在您的代码风格中:

header(sprintf("Location: %s", $insertGoTo));

【讨论】:

    猜你喜欢
    • 2016-09-22
    • 1970-01-01
    • 2016-10-05
    • 2013-04-13
    • 1970-01-01
    相关资源
    最近更新 更多