Question

<html>
    <body>

    <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
        Name: <input type="text" name="fname">
        <input type="submit">
    </form>

    <?php
        $name = $_REQUEST['fname'];//Notice is coming at this line
        echo $name;
    ?>

    </body>
</html>

The above code shows Notice like this:-

Notice: Undefined index: fname in C:\xampp\htdocs\Programs\request.php on line 10

How to remove this error? I took this code from w3schools.com. It is running fine in w3schools.com site. In my PC also it runs but when I open this program in my browser, it shows the above line. Plz help me..

Was it helpful?

Solution 2

PHP gives notice when variable are not defined like in your case $_REQUEST['fname'] is not set/defined.

Change to:

$name = isset($_REQUEST['fname']) ? $_REQUEST['fname'] : "";//better way

OR you can do

$name = @$_REQUEST['fname'];//this way notice or warnings are suppressed

OTHER TIPS

it gives this notice when $_REQUEST['fname'] is not set.

Change:

$name = $_REQUEST['fname'];//Notice is coming at this line

to:

$name = isset($_REQUEST['fname']) ? $_REQUEST['fname'] : "";

you need to check that if it is set or not by isset()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top