简体   繁体   English

无法从表单获取用户名(使用“ post”)

[英]Cannot get username from form (using "post)

First of all I apologize for the noobish nature of my question. 首先,我对问题的笨拙性质表示歉意。 I have tried to search the internet for a solution, so far with little luck. 到目前为止,我已经尝试在互联网上寻找解决方案,但运气不佳。 I therefore turn to the awesome people of StackOverflow, as this usually gets the problems solved quite fast. 因此,我将转向StackOverflow的优秀人员,因为这通常可以很快解决问题。

I am trying to make a very simple login function for a website. 我正在尝试为网站创建一个非常简单的登录功能。 It consists of a HTML-form and a php login script. 它由HTML表单和php登录脚本组成。 The problem is that the username (and password) does not seem to get passed from the form to the login script. 问题在于用户名(和密码)似乎没有从表单传递到登录脚本。 Starting with the form, it looks like this: 从表单开始,它看起来像这样:

<form name="loginForm" method="post" action="loginScript.php">
    <table style="position: absolute; top:55%; left:50%; transform: translate(-50%, -50%)">
        <tr>
            <td>Brugernavn:</td><td><input name="edUsername" type="text" id="username" autofocus></td>
        </tr>
        <tr>
            <td>Adgangskode:</td><td><input name="edPassword" type="password" id="password"><br></td>
        </tr>
        <tr>
            <td style ="font-size: 10px;">Glemt din adgangskode?</td>
            <td style="text-align: right;"><input name="Submit" type="submit" value="Log ind"></td>
        </tr>
    </table>
</form>

So the information i need in my login script is the username and password data. 因此,我在登录脚本中需要的信息是用户名和密码数据。 I try to obtain these with the filter_input() since I read that this was safer than $_POST[]. 我试着通过filter_input()获得它们,因为我读到这比$ _POST []更安全。 In the login script it looks like this: 在登录脚本中,它看起来像这样:

// Get username and password from post form
$ed_user = filter_input(INPUT_POST, 'username');
$ed_pass = filter_input(INPUT_POST, 'password');
echo $ed_user;

The "echo" part is just a temporary line, to check if there is indeed a username retrieved. “ echo”部分只是一个临时行,用于检查是否确实找到了用户名。 So far this is not the case. 到目前为止,情况并非如此。 So my question is, what the heck am I doing wrong? 所以我的问题是,我到底在做什么错?

Rewrite as 改写为

$ed_user = filter_input(INPUT_POST, 'edUsername');
$ed_pass = filter_input(INPUT_POST, 'edPassword');

Use the name of the field instead of the id. 使用字段名称而不是ID。

Your mistake is that you're using input's id attribute instead of the name . 您的错误是您使用输入的id属性而不是name

// Get username and password from post form
$ed_user = filter_input(INPUT_POST, 'edUsername');
$ed_pass = filter_input(INPUT_POST, 'edPassword');
echo $ed_user;

Or you could swap the attributes so the PHP code is left as is. 或者,您可以交换属性,使PHP代码保持不变。

As for the differences between id and name attributes check out another question . 至于idname属性之间的差异,请查看另一个问题

You've got the names wrong, Check below. 您输入的名称有误,请检查以下内容。

$ed_user = filter_input(INPUT_POST, 'edUsername');
$ed_pass = filter_input(INPUT_POST, 'edPassword');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM