获取在PHP中“回显”的HTML输入框的字段值

时间:2014-09-12 22:55:00

标签: php html

我正在尝试创建登录/激活页面(作为分配要求的一部分),如果激活成功,则该人员会看到登录页面。我在PHP块中有登录页面的HTML代码(因为我不知道隐藏表单的最安全,最安全的方法),并且想知道如何获取用户输入用户名的信息,密码字段。

这是我的代码:

<?php
/* Project information:
 * Author: Michael Warren
 * FileName: login.php
 * Creation Date: 9/12/2014
 */
// create variables for userName (or email address), password, needsActivation, activationCode
$userName = "";
$password = "";
$needsActivation = ((isset($_POST["activate"])) ? (bool)($_POST["activate"]) : false);
$activationCode = "";

// variable to check if the current user of this page has been activated
$userActivated = !($needsActivation);

// JavaScript messages that tell the user the status of their activation
$activationSuccess = '<script>alert("Activation successful! Bringing up the log in page.");</script>';
$activationFailed = '<script>alert("Activation failed. You have been sent another activation e-mail.");</script>';

// JavaScript messages that tell the user about the login
$missingUserName = '<script>alert("Username required.");</script>';
$missingPassword = '<script>alert("Password required");</script>';
$invalidUserName = '<script>alert("Invalid username entered.");</script>';
$invalidPassword = '<script>alert("Invalid password entered.");</script>';
$successfulLogin = '<script>alert("You have successfully logged in!");</script>';

if ($needsActivation)
{   
    // get the information from the query string (or the POSTed data)
    $userName = $_POST["name"];
    $password = $_POST["password"];
    $activationCode = $_POST["activationCode"];
    // check the activationCode
    // if the activationCode checks out (in this project, that just means that the length of the activation code is 50 characters)
    if (strlen($activationCode) == 50)
    {
        // user has been activated
        $userActivated = true;
        // send email and/or JavaScript message telling them that
        echo $activationSuccess;
        mail($userName, 
            "Activation Successful",
            <<<HERE
Your activation was a success. You can now use this site. Your login credentials are as follows:

UserName: $userName
Password: $password
HERE
);
        }
        // otherwise
        else
        {
            // deny access to login page
            // send email to the user to try again, and inform them that it has been done
            echo $activationFailed;
        }
    }
    if ($userActivated)
    {
        // show the login page
        echo <<<HERE
        <div id="contents">
            <div id="clearfix">
                <div id="main">
                    <h1>Login</h1>
                    <p>Log in to the site using your e-mail as your password.</p>
                    <form class="message">
                        <label>
                            <p class="descriptor">User name: </p><input type="text" name="userNameField" value=""></input>
                        </label>
                        <br>
                        <label>
                            <p class="descriptor">Password: </p><input type="password" name="passwordField" value=""></input>
                        </label>
                        <br>
                        <input type="submit" name="LogInButton" value="Log in"></input>
                    </form>
                </div>
            </div>
        </div>
HERE;
    }

    function checkLogin()
    {

    }
?>
    <body>
</html>

我打算尝试使用按钮触发checkLogin(),但第一步是知道如何检索该用户输入。这就是我被困住的地方。

1 个答案:

答案 0 :(得分:1)

如果您打算将表单发布回PHP,请在表单上设置一个操作属性并通过提交按钮发布,在服务器端,您可以从$ _POST数组中访问这些值。

如果您想在客户端捕获这些值,请使用jQuery或纯JavaScript,可以为输入分配一些ID,以便轻松定位它们。