0

I have this code :

<?php
if ($_GET["do"]=="success") {
    echo "SUCCESS";
}
?>

<form action="file.php?do=success" method="post">
<input type="text"><input type="submit">
</form>

And I get this error:

Notice: Undefined index: do in C:\Program Files\EasyPHP-5.3.8.1\www\m\file.php on line 2

What do I need to do?

7
  • You might want to take a glance at PHP error_reporting Commented Nov 15, 2011 at 22:27
  • 1
    @budwiser: isn't the error already reported? Or are you suggesting to suppress the errors and pretend like it never happened? :P Commented Nov 15, 2011 at 22:29
  • @budwiser Given that the OP is seeing E_NOTICE errors, I'd say he's got it under control Commented Nov 15, 2011 at 22:29
  • Truthfully, that error isn't the end of the world. But with PHP5.3 we are having to code more strict. This is a good thing overall, but just make sure the var exists before moving down the code. Commented Nov 15, 2011 at 22:31
  • 1
    @DavidGuerra: Did you just say what I think you did? :o Suppressing errors / notices is NEVER a good idea. Either coding strict or not. Might be just be getting old though. Although I don't think so :D Commented Nov 15, 2011 at 22:34

2 Answers 2

3

You need to check if the index exists before trying to access it:

<?php
if (isset($_GET["do"]) && $_GET["do"] == "success") {
    echo "SUCCESS";
}
?>

<form action="file.php?do=success" method="post">
<input type="text"><input type="submit">
</form>
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

<?php
if (!empty($_GET['do']) && $_GET["do"]=="success") {
    echo "SUCCESS";
}
?>

<form action="file.php?do=success" method="get">
<input type="text"><input type="submit">
</form>

4 Comments

@RobertasPalinskis use isset() rather than empty(). empty doesn't check if the value was set and returns errors on unset values. null is not "not set";
@UğurGümüşhan this is incorrect. empty() does test for the existence of an object. Review example 1A or test it in code: php.net/manual/en/function.empty.php
I know the documentation. "not set" is not NULL
"empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set." Empty will return no errors if a value is not set per the documentation. So, if empty is not set or has no value, then the conditions are not met and no errors are produced.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.