user3678471
user3678471

Reputation: 2433

Notice: Undefined index php $_request

I need eliminate this warning with an if sentence I tried to use if $_REQUEST['fname'] but it not worked. I'm new in PHP

<!DOCTYPE html>
<html>
<body>

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

<?php 
if($_REQUEST['fname']){
 $name = $_REQUEST['fname']; 
 echo $name; 
}
?>

</body>
<html>

Browser

Notice: Undefined index: fname in /var/www/php_functions.php on line 11

Upvotes: 0

Views: 5698

Answers (3)

Manwal
Manwal

Reputation: 23836

You can simply check this by isset()

Try this code:

if(isset($_REQUEST['fname'])){
 $name = $_REQUEST['fname']; 
 echo $name; 
}

Upvotes: 4

Kevin
Kevin

Reputation: 41873

Yes, $_REQUEST variable also contains $_POST. But the way you are checking it is incorrect.

It can be done using isset() to check its existence:

if(isset($_REQUEST['fname'])){
    $name = $_REQUEST['fname']; 
    echo $name; 
}

Ref: http://php.net/manual/en/reserved.variables.request.php

Upvotes: 1

Rizier123
Rizier123

Reputation: 59701

It's not the REQUEST array you have to use! It's POST since your method of the form is post:

like this:

$_POST['fname']

And i think you want to check if it is set like this:

if(isset($_POST['fname']))

Upvotes: 1

Related Questions