PHP can not read POST sent by JavaScript

3

I'm creating a login system for my site following tutorial , but I have a problem when I log in. Apparently, the JavaScript code does not pass the form parameters correctly. The login page code is:

function hex_sha512(value) {
    // apenas para simular
    return 'hashed-value';
}

function formhash(form, password) {
    // Crie um novo elemento de input, o qual será o campo para a senha com hash. 
    var p = document.createElement("input");

    // Adicione um novo elemento ao nosso formulário. 
    form.appendChild(p);
    p.name = "p";
    p.type = "hidden";
    p.value = hex_sha512(password.value);

    // Cuidado para não deixar que a senha em texto simples não seja enviada. 
    password.value = "";

    // Finalmente, envie o formulário.
    form.submit();
}
<form action="includes/process_login.php" method="POST" name="login_form">
  <div class="form-group">
    <input class="form-control" placeholder="E-mail" name="email" type="email" autofocus>
  </div>
  <div class="form-group">
    <input class="form-control" placeholder="Password" name="password" type="password" value="" id="password">
  </div>
  <div class="checkbox">
    <label>
      <input name="remember" type="checkbox" value="Remember Me">Remember Me
    </label>
  </div>
  <!-- Você também pode alterar este botão para um input como opção de envio -->
  <input type="button" value="Login" onclick="formhash(this.form, this.form.password, this.form.p);" class="btn btn-lg btn-success btn-block"/>
</form>
For some reason, when I test on the process_login.php page, if the variables $_POST['email'] and $POST['p'] are set, it returns false, as if they were not set.

process_login.php :

    if (isset($_POST['email'], $_POST['p'])) {
    $email = $_POST['email'];
    $password = $_POST['p'];

    if (login($email, $password, $mysqli) == true) {
        // Login com sucesso
    } else {
        // Falha de login 
    }
} else {
    // As variáveis POST corretas não foram enviadas para esta página. 
    echo 'Invalid Request';
}

The system always falls into the "Invalid Request".

What could it be?

    
asked by anonymous 21.10.2015 / 01:51

1 answer

-1

Do you need to use JavaScript? If you do not need to, you can do the following:

Login page

<form action="includes/process_login.php" method="POST">
    <div class="form-group">
            <input class="form-control" placeholder="E-mail" name="email" type="email" autofocus>
    </div>
    <div class="form-group">
            <input class="form-control" placeholder="Password" name="password" type="password" value="" id="password">
    </div>
    <div class="checkbox">
      <label>
         <input name="remember" type="checkbox" value="Remember Me">Remember Me
     </label>
    </div>
     <!-- Você também pode alterar este botão para um input como opção de envio -->
     <button type="submit" class="btn btn-lg btn-success btn-block"/>Login</button>
</form>

Page: process_login.php

<?php
$email = $_POST['email'];
$password = $_POST['password'];
?>

In this way, you are submitting the form with the POST method, and receiving the form inputs on the other page with the same method.

I use this way because I have not yet mastered JS functions very much and I believe that it can meet your needs.

Just remembering that this way there is no hash, but apparently you already know how to do. If you need any more material, follow: How to do password hash safely?

    
21.10.2015 / 05:38