PHP表单,包含2个提交按钮,条件作品,但没有标题重定向

时间:2013-09-02 06:35:32

标签: php forms submit-button

我在网页上有一个登录表单,我希望用户能够登录或注册。在页面的顶部,我有PHP脚本和条件测试,按下了提交按钮。登录部分工作正常。 “注册”部分有效(意思是,它进入if语句),但它没有重定向到我的register.php站点。有什么想法吗?

我已经尝试更改if / elseif语句的顺序,但无济于事。我没有得到这个脚本的警告,但只是重新加载页面而不是重定向到register.php。

要查看实际网页,请转到here

<?php
$error = ''; 
session_start();
ob_start();
if (isset($_POST['register'])) {
    header("http://www.mynextbit.com/authenticate/register.php");
}
elseif (isset($_POST['login'])) { 
    session_start(); 
    $username = trim($_POST['username']);
    $_SESSION['user'] = $username;
    $password = trim($_POST['pwd']); 
    // location to redirect on success 
    $redirect = 'http://www.mynextbit.com/Pages/newtemplateTEST.php'; 
    require_once('../includes/authenticate_mysqli.inc.php'); 
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
...

并且,HTML FORM ...

<?php
if ($error) {
  echo "<p>$error</p>";
}
if (!$_SESSION['user']) { ?>
    <font color="red"><i><userlogin>Please Log In</userlogin></i></font><br />
    <form id="form1" method="post" action="" style="margin-left: 15px">
    <label for="username" ><font size="-1">Username:</font></label>
    <input type="text" name="username" id="username">
    <br />
    <label for="pwd"><font size="-1">Password:</font></label>
    <input type="password" name="pwd" id="pwd">
    <br />
    <input name="login" type="submit" id="login" value="Log in" style="display:inline"> or
    <input name="register" type="submit" id="register" value="Register" style="display:inline">
    </form>

<?php } ?>

3 个答案:

答案 0 :(得分:3)

点击这里。 http://php.net/manual/en/function.header.php 使用带有位置的标题,如header('Location: http://www.example.com/');

header('Location:http://www.mynextbit.com/authenticate/register.php');

它应该有用。

由于

答案 1 :(得分:0)

试试这个:

if (isset($_POST['register'])) {
    header("Location:http://www.mynextbit.com/authenticate/register.php");
}

请参阅Reference

答案 2 :(得分:0)

或许,根据我从PHP documentation看到的,HEADER函数必须具有这样的“位置”?

<?php
header("Location: http://www.example.com/"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>

这应该可以解决您的问题,我尝试过它实际上重定向:P

另外(不是真的需要,但有时候推荐),如果你不想有太多的php标签开启和关闭器,你可以使用HEREDOCS。

代码示例:

<?php
if ($error) {
  echo "<p>$error</p>";
}
if (!$_SESSION['user']) { 
    echo <<<FORM
    <font color="red"><i><userlogin>Please Log In</userlogin></i></font><br />
    <form id="form1" method="post" action="" style="margin-left: 15px">
    <label for="username" ><font size="-1">Username:</font></label>
    <input type="text" name="username" id="username">
    <br />
    <label for="pwd"><font size="-1">Password:</font></label>
    <input type="password" name="pwd" id="pwd">
    <br />
    <input name="login" type="submit" id="login" value="Log in" style="display:inline"> or
    <input name="register" type="submit" id="register" value="Register" style="display:inline">
    </form>
FORM;
}
?>