PHP - How to make sure that if a POST is not entered, it does not appear "Notice: Undefined index"

Asked

Viewed 163 times

1

It is possible to make sure that if I do not enter data in a form with method="get", when you go to action="/minha-url.php", if you have not entered data in the previous form, simply nothing will be displayed instead of "Notice: Undefined index", etc...

Part where I make the GET content appear:

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

When I input the email, it stays: [email protected]

When I don’t enter, it stays: Notice: Undefined index: email in C: xampp htdocs teste.php on line 104


It’s a very simple question, but I couldn’t find solutions, I couldn’t find anything on the Internet to solve this problem.

  • 2

    this is one of the most recurrent subjects. are you sure you searched the internet? http://answall.com/questions/21714/como-resolver-um-notice-undefined-index

  • 1

    http://answall.com/q/87988/4793

  • 2

    Use isset($_GET) or isset($_GET["email"]). Type: <?= isset($_GET["email"]) ? $_GET["email"] : ''>;

  • 1

    http://answall.com/q/89960/4793

3 answers

2

Could use the command isset to test if the value has been reported.

In this way:

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

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

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

Read more about using or not using arroba ("@") at: Why do they say using @arroba to suppress errors is a bad practice?

1

there are ways to avoid the appearance of this error message, but, I do not know for sure the focus of its application that is developing, a good practice is the validation of data entry, which can be done both in HTML, JS or PHP, how you are performing a simple submission using the attribute action, I recommend that you already validate HTML, thus:

<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 the method POST and is using the supervariable $_GET[], what is wrong, the right is if method GET soon $_GET[], or if method POST soon $_POST[] .

1

Use the function filter_input, she gets a constant INPUT_GET or INPUT_POST:

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

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

Browser other questions tagged

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