Reputation: 13
<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..
Upvotes: 0
Views: 478
Reputation: 1875
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
Upvotes: 0
Reputation: 9351
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()
][1]
[1]:https://www.php.net/isset
Upvotes: 2