PHP Undefined Variable - I can’t assign a value to POST

Asked

Viewed 169 times

-2

The goal is to log in using the CPF or NIS number. However I am not able to assign values by the POST method to the $CPF and $NIS so the login is never performed.

<form class="form" method="POST" action="verificacao.php">


                    <label class="label-input" for="">
                        <i class="fas fa-lock icon-modify"></i>
                        <input type="number" name="CPF" placeholder="CPF" required="required">
                    </label>

                    <label class="label-input" for="">
                        <i class="fas fa-lock icon-modify"></i>
                        <input type="number" name="NIS" placeholder="NIS" required="required">
                    </label>

                    <button type="submit" class="btn btn-second" name="send">Entrar</button>
    </form>
```

Abaixo o restante do código em PHP. Não consigo passar nem mesmo do primeiro "if". Quando tento printar o Array do $POST, ele aparece vazio. 

```
<?php

 print_r($_POST);
     if(isset($_POST['send'])){
     if (isset($_POST['CPF']) || isset($_POST['NIS']))
     {   
        echo "Chegou aqui1" ;

         //verificar se esta preenchido

         if(!empty($CPF) || !empty($NIS))
         {  
            $CPF = addslashes($_POST['CPF']);
            $NIS = addcslashes($_POST['NIS']);

            $u->conectar("cadastrados","localhost", "root","root");
            if($u->msgErro == "")
            {
                if($u->logar($CPF,SNIS))
                {
                    header("location: verificacao.php");
                    echo "Chegou aqui5" ;                   
                }
                else
                {
                    echo "CPF e NIS não encontrados!" ;
                    echo "Chegou aqui4" ;
                }
            }
            else
            {
                echo"Erro: ".$u->msgErro;
                echo "Chegou aqui3" ;
            }   
         } 
         else{
             echo "Preencha com número do CPF ou NIS!"; 
             echo "Chegou aqui2" ;
         }

     }
     } 
     ?> 
    ```

Preciso muito de ajuda, se alguém puder me dar uma luz serei muito grato!
  • 2

    That’s whyif(isset($_POST['send'])) you did not define a value for <button name='send'>, then isset($_POST['send']) will always be false. Either you delete in PHP this check or in HTML you define a value for element.

  • https://answall.com/help/someone-answers

  • Hello, Jonatas! If the answer was helpful, please accept the answer! Thank you!

2 answers

1

Hello,

Our friend Augusto Vasques is correct in your comment!

You are checking whether the variable $_POST['send'] is set, but it is not being sent via POST as it does not contain a defined value (value)!

1) Set a value for the "send button"

<button type="submit" class="btn btn-second" name="send" value="entrar">Entrar</button>

2) Or check only the $_POST variable

if( isset($_POST) ){

Anyway, I suggest researching something to increase the security of your form!

  • Hello, Jonatas! If the answer was helpful, please evaluate correctly! Thank you

  • The isset checks if a variable has been defined/initialized, it is not checked if it has a value. The isset will always return true to $_POST because it will always be set

  • @Leocaracciolo if the HTML form is not VALUE set, the browser does not send the field in the POST... ai will never have $_POST['send']...

  • Good, OK then rs not even tested here! I already went through this situation a long time ago and preferred by default always set a value (but I think it was not with the button my problem), it may be that things have changed or I’m confusing myself even! Thank you

  • And apparently the author of the question is absent from the rss community

  • All right, I think you’ve confused with NAME, if you don’t have a name in the Ubmit input then it won’t work. In this case, option 2 of your answer is better. if( isset($_POST) ){

  • Humm can be that yes rs long ago!! And as used Jquery with selector by ID, I should have forgotten in the name! I am a little cautious, would use: if( isset($_POST['send']) && $_POST['send'] === 'enter' )) rs

Show 2 more comments

0

Your code

  ........
  ........
 if(!empty($CPF) || !empty($NIS))
     {  
        $CPF = addslashes($_POST['CPF']);
        $NIS = addcslashes($_POST['NIS'])

  .......
  .......

Correct

 .......
 .......
 $CPF = addslashes($_POST['CPF']);
 $NIS = addcslashes($_POST['NIS'])

 if(!empty($CPF) || !empty($NIS))
     {  

  .......
  .......

So if any value is sent to CPF and/or NIS will no longer be empty and will enter the if

Browser other questions tagged

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