如果填写了一个输入框,则标题不更改URL

时间:2019-02-09 18:17:22

标签: php html

我一直在使用html,css和PHP的网站上工作。目前,我正在尝试这样做,以便当一个人尝试注册时,例如,将用户名保留为空,URL错误应该类似于http://localhost/Presence/PresenceSignup.php?error=emptyfields%uid=&mail=123123。但是URL保持不变 http://localhost/Presence/PresenceSignup.php?error=emptyfields%uid=&mail=

<?php
    require "PresenceNavbar.php"
?>
<link href="css/SignUp.css" rel="stylesheet" type="text/css">
<body scroll="no" style="overflow: hidden">
    <main>
        <div>
            <section class ="SignupSheet">
                <img src="assets/Keyhole.png" width="130" class="avatar">
            <h1>Sign Up</h1>
            <form action="includes/signup.inc.php" method="post">
                <p>Username</p>
                <input type="text" name="uid" placeholder="Username">
                <p>E-mail</p>
                <input type="text" name="mail" placeholder="E-mail">
                <p>Password</p>
                <input type="password" name="pwd" placeholder="Password">
                <p>Repeat Password</p>
                <input type="password" name="pwd-repeat" placeholder="Repeat Password">
                <input type="submit" name="signup-submit" id="signup-submit" value="Sign Up">
            </form>
            </section>
        </div>

    </main>
</body>
<?php

if (isset($_POST['signup-submit']))
{
    require 'dbh.inc.php';


    $username = $_post['uid'];
    $email = $_post['mail'];
    $password = $_post['pwd'];
    $passwordrepeat = $_post['pwd-repeat'];

    if (empty($username) || empty($username) || empty($username) || empty($username))
    {
        header("Location: ../PresenceSignup.php?error=emptyfields%uid=".$username."&mail=".$email);
        exit();
    }

}
<?php

$servername = "localhost";
$dBUsername = "root";
$dBPassword = "";
$dBName = "loginsystem";

$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBName);

if (!$conn)
{
    die("Connection Failed: ".mysqli_connect_error());
}

2 个答案:

答案 0 :(得分:1)

它应该是&而不是%$_POST而不是$_post。试试这个:

<?php

if (isset($_POST['signup-submit']))
{
    require 'dbh.inc.php';


    $username = $_POST['uid'];
    $email = $_POST['mail'];
    $password = $_POST['pwd'];
    $passwordrepeat = $_POST['pwd-repeat'];

    if (empty($username) || empty($username) || empty($username) || empty($username))
    {

        header("Location: ../PresenceSignup.php?error=emptyfields&uid=".$username."&mail=".$email);
        exit();
    }

}

答案 1 :(得分:0)

请勿像这样手动构建您的URL。这是非常不安全且容易出错的。 PHP为此提供了解决方案。使用http_build_query

作为示例,您可以通过以下方式使用它:

$username = $_POST['uid'];
$email = $_POST['mail'];
$password = $_POST['pwd'];
$passwordrepeat = $_POST['pwd-repeat'];

if (empty($username)) {
    $queryStringArray = [
        'error' => 'emptyfields',
        'uid' => $username,
        'mail' => $email,
    ];
    header("Location: ../PresenceSignup.php?".http_build_query($queryStringArray));
    exit();
}

此外,如前所述,但其他情况是$_POST而不是$_post,您检查了4次empty($username)