【问题标题】:Failing to get POST values from HTML form in PHP无法从 PHP 中的 HTML 表单获取 POST 值
【发布时间】:2021-08-31 04:10:17
【问题描述】:

我正在努力使用 POST 将 HTML 表单中的值转换为 PHP 变量。它曾经可以工作,但现在无论我做什么,它都不起作用。这是php文件:

<?php
    include "DBConn.php";

    session_start();
?>

<html>
    <head>
        <title>Register</title>
        <link rel="stylesheet" href="Register.css" type = "text/css">
    </head>

    <body>
        <div id="container">       
            <form action="register.php" method = "post">
                <label for="name">Name:</label>
                <input type="text" id="name" name="txtName" value = <?php if(isset($_POST['txtName'])) echo $_POST['txtName'];?>>

                <label for="surname">Surname:</label>
                <input type="text" id="surname" name="txtSurname" value = <?php if(isset($_POST['txtSurname'])) echo $_POST['txtSurname'];?>>

                <label for="address">Address:</label>
                <input type="text" id="address" name="txtAddress" value = <?php if(isset($_POST['txtAddress'])) echo $_POST['txtAddress'];?>>

                <label for="email">Email:</label>
                <input type="email" id="email" name="txtEmail" value = <?php if(isset($_POST['txtEmail'])) echo $_POST['txtEmail'];?>>

                <label for="password">Password:</label>
                <input type="password" id="password" name="txtPassword" value = <?php if(isset($_POST['txtPassword'])) echo $_POST['txtPassword'];?>>

                <label for="password2">Re-enter Password:</label>
                <input type="password" id="password2" name="txtPassword2" value = <?php if(isset($_POST['txtPassword2'])) echo $_POST['txtPassword2'];?>>

                <div id="lower">
                    <input type="submit" value="Register" name = "btnRegister">
                </div><!--/ lower-->
            </form>
        </div>
    </body>
</html>

<?php
    //Runs if btnRegister is clicked. Registers a user.
    if (isset($_POST['btnRegister'])) {
        //Assigns form data to variables
        $_SESSION["Name"] = $_POST['txtName'];
        $_SESSION["Surname"] = $_POST['txtSurname'];
        $_SESSION["Email"] = $_POST['txtEmail'];
        $_SESSION["Password"] = $_POST['txtPassword'];
        $_SESSION["Password2"] = $_POST['txtPassword2'];
        $_SESSION["Address"] = $_POST['txtAddress'];

        $sqlSelect = 
        "SELECT *
        FROM tbl_Customer
        WHERE Email = '{$_SESSION["Email"]}'";

        //Runs select query
        $result = $conn->query($sqlSelect);
        $md5pass = md5($_SESSION["Password"]);

        //Checks to see if user exists based on query
        if ($result->num_rows == 0) {
            //Checks to see if passwords match
            if ($_SESSION["Password"] == $_SESSION["Password2"]) {
                //Passwords match
                echo '<script>alert("Passwords match")</script>';
                
                //Insert statement to insert user into table
                $sqlInsert =    "INSERT INTO tbl_Customer (Name, Surname, Email, Password, Address)
                                 VALUES ('{$_SESSION["Name"]}','{$_SESSION["Surname"]}','{$_SESSION["Email"]}','$md5pass','{$_SESSION["Address"]}');";

                if ($conn->query($sqlInsert) === TRUE) {
                    echo '<script>alert("Registered successfully")</script>';
                } 
                else {
                    echo '<script>alert("Registration error: " . $sql . "<br>" . $conn->error)</script>';
                }
            }
            else {
                echo '<script>alert("Passwords do not match")</script>';
            }
        } 
        else {
            //User exists
            echo '<script>alert("User already exists, choose a different email.")</script>';
        }

        header('Location: login.php');
        exit();
    }

 ?>

所有警报都不起作用,表明它们回显不起作用。此外,第二次单击注册按钮时,我将进入登录页面,这意味着注册按钮必须正常工作。它不会给我任何错误或消息。

【问题讨论】:

标签: php html post phpmyadmin xampp


【解决方案1】:

如果您的 PHP 代码与 HTML 代码位于同一页面上,请从 &lt;form&gt; 标记中删除 action="register.php"action 属性指定提交表单时将表单数据发送到何处,因为您的 PHP 代码在同一页面上,您应该删除 action 并且表单数据将在同一页面上提交。

或者

创建一个register.php 并将 PHP 代码放在那里。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-02
    • 2019-03-19
    • 1970-01-01
    • 2015-03-11
    相关资源
    最近更新 更多