PHP - How to ensure that if a POST is not inserted, "Notice: Undefined index"

1
  

If I do not enter data on a form with method="get" , when I go to the action="/minha-url.php" page, if you did not enter data in the previous form, you will simply not see anything, instead of "Notice : Undefined index ", etc ...

Part where I submit the content of the GET:

<?php echo  ($_GET["email"]) ; ?>
  

When I enter the email, it is: [email protected]

     

When I do not enter, it is: Notice: Undefined index: email in C: \ xampp \ htdocs \ teste.php on line 104

It's a simple question, but I did not get any solutions, I did not find anything on the internet that solved this problem.     
asked by anonymous 26.08.2016 / 08:27

3 answers

2

You could use the isset command to test whether the value was reported .

In this way:

<?php 
if(isset($_GET["email"])){
    echo  ($_GET["email"]) ; 
}
?>

Note: You'll notice that a lot of people use the at ("@") to omit errors, but it can cause a big headache in a situation where you need to find the errors and they do not appear because they are omitted. It would look like this:

<?php 
echo  @($_GET["email"]); 
?>

Read more about whether or not to use the at sign ("@") at: Why they say use Is it a bad practice to suppress mistakes?

    
26.08.2016 / 13:14
1

There are ways to avoid the appearance of this error message, but, I'm not sure the focus of your application that is developing, a good practice is the validation of data entry, which can be done in HTML JS or PHP , as you are performing a simple submission using the action attribute, I recommend that you already validate by HTML , like this:

<form method="post" action="" >
    <input type="email" name="email" required />
</form>

One more thing I noticed is that by your description you are sending data with POST method and you are using the $_GET[] supervariable, which is wrong, the right method is GET logo $_GET[] , or method POST logo $_POST[] .

    
26.08.2016 / 13:05
1

Use the filter_input function, it receives a constant INPUT_GET or INPUT_POST :

<?php
echo filter_input(FILTER_POST, 'email');

This function also allows you to apply filters to the read variable, see more information on Types of filter (documentation page without translation)

    
26.08.2016 / 14:00