0

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.

I am trying to make a very simple login function for a website. It consists of a HTML-form and a php login script. 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[]. 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. So far this is not the case. So my question is, what the heck am I doing wrong?

Jakob Busk Sørensen
  • 4,592
  • 4
  • 32
  • 64

3 Answers3

4

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.

Barry
  • 3,435
  • 1
  • 14
  • 24
  • I tried that too, with the same result (nothing seems to get passed). Can you tell me the difference between "id" and "name" of a form field? – Jakob Busk Sørensen Oct 19 '14 at 14:13
  • @Noceo you're using wrong variable to access POST data. – uKolka Oct 19 '14 at 14:15
  • 1
    The id uniquely identifies the html element within the document. And can be used as a reference for css and jquery. Names are mostly used within forms to reference them for post and get actions. You can even assign names like 'name[]' to be able to send the values from different fields as an array. – Barry Oct 19 '14 at 14:17
  • Update, changing the variable might actually have helped, think there was a delay in the update of my files. I would still like to know the difference though :-). – Jakob Busk Sørensen Oct 19 '14 at 14:17
  • You beat my to it. It is working now, thank you (all of you) for your assistance. – Jakob Busk Sørensen Oct 19 '14 at 14:19
1

Your mistake is that you're using input's id attribute instead of the 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.

As for the differences between id and name attributes check out another question.

Community
  • 1
  • 1
uKolka
  • 27,488
  • 4
  • 29
  • 43
  • I was going to say about $_POST but then I checked the filter_input manual on [PHP.NET](http://php.net/manual/en/function.filter-input.php) and INPUT_POST is correct – Billy Oct 19 '14 at 14:35
1

You've got the names wrong, Check below.

$ed_user = filter_input(INPUT_POST, 'edUsername');
$ed_pass = filter_input(INPUT_POST, 'edPassword');
Billy
  • 2,390
  • 1
  • 7
  • 13