Error sending form by pressing Enter - PHP

Asked

Viewed 57 times

0

good evening. I’m having a problem submitting a form, typing Enter returns the error: php’s "Undefined Index". However, if I click the Login button, it normally submits.

inserir a descrição da imagem aqui

< script type = "text/javascript" >
  $(function() {
    $('.form').form({
      fields: {
        usuario: {
          identifier: 'usuario',
          rules: [{
            type: 'empty',
            prompt: 'Preencha o campo Usuário!'
          }]
        },

        senha: {
          identifier: 'senha',
          rules: [{
            type: 'empty',
            prompt: 'Preencha o campo Senha!'
          }]
        }
      }
    });
  }); <
/script>
* {
  margin: 0px;
  padding: 0px;
  border: none;
  outline: none !important;
}

body {
  background-color: #f4f4f4 !important;
}

.login-alerts {
  width: 350;
  height: 50px;
  margin: 0 auto;
}

.login-center {
  width: 350px;
  height: auto;
  background-color: #ffffff;
  margin: 0 auto;
  margin-top: 8%;
  box-shadow: 1px 2px 8px 1px #d6d6d6;
  border-radius: 6px;
}

.login-logo {
  padding: 20px 0px 0px 20px;
}

.login {
  padding: 10px 20px 20px 20px;
}
<link href="https://raw.githubusercontent.com/Semantic-Org/Semantic-UI/master/dist/semantic.min.css" rel="stylesheet" />
<script src="https://raw.githubusercontent.com/Semantic-Org/Semantic-UI/master/dist/semantic.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>


<body>
  <div class="login-center">
    <div class="login-top">
      <div class="login-logo"><img src="assets/img/sigio-logo.svg" width="150"></div>
    </div>

    <div class="login">
      <form class="ui form error" action="" method="POST">
        <div class="field ui fluid left icon input">
          <input type="text" placeholder="Usuário" id="usuario" name="usuario" maxlength="15">
          <i class="user icon"></i>
        </div>

        <div class="field ui fluid left icon input">
          <input type="password" placeholder="Senha" id="senha" name="senha" maxlength="25">
          <i class="lock icon"></i>
        </div>

        <div class="field">
          <input type="submit" class="fluid ui primary button" name="logar" value="Logar">
        </div>

        <div class="ui error message"></div>
      </form>
    </div>
  </div>
</body>

</html>

  • You should also put the PHP code, after all the error is in it. Ps.: To fix add the field logar in the Javascript.

1 answer

0

Following what the message says, some variable with the name "log in" has not been defined.

Try to use something like:

if( isset($_POST['logar']) ){
   // Seu código
}

This will cause the existence of the "log in" field to be verified and the "notice" message is not displayed because of this. When the existence of the same is not verified and the PHP interpreter sends the E_NOTICE .

An alternative would be to put a @ before starting in the variable name, as follows:

@$logar = $_POST['logar'];
//ou
$logar = @$_POST['logar'];

However, it is not recommended that this be done because it hides errors of the type E_NOTICE mainly it is gambiarra. What one should do is to surround all the possibilities of errors, generating personalized messages.

That is why the existence check was necessary.

And to finish, you don’t need to set a value for the button that sends the form data.

Setting the button name is enough.

<input type="submit" class="fluid ui primary button" name="logar">

Browser other questions tagged

You are not signed in. Login or sign up in order to post.