-1

this is the code for my html page in the site:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>index</title>
</head>

<body>
<html>
<body>

<form action="Untitled-4.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

</body>
</html> 
</body>
</html>

and this is my code for the "Untitled-4.php" file:

<html>
<body>


<?php 

if ($_GET["name"] == "tom"){
echo "hello you are special";
}
?>

</body>
</html> 

this is the error i am getting:

"undefined index on line 7 of the php file"

I am trying to use the GET method to take whatever the user types in the "name" box to be used in the if statement that echoes "you are special" if the user types in "tom". can anyone tell me what the problem here is.

(the question stack overflow is saying is a possible duplicate is a completely different question)

Martin
  • 22,212
  • 11
  • 70
  • 132
  • @Epodax- the php file is Untitled-4.php i will edit the question now. – webdevelopert Aug 16 '16 at 11:57
  • 2
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – CBroe Aug 16 '16 at 11:57
  • Also, when you submit your form do you see `?name=tom` (if you wrote in tom in the input field), at the end of the URL in your address bar? – Epodax Aug 16 '16 at 11:59

2 Answers2

0

Check if the variable is set first:

<html>
<body>


<?php 
if (isset($_GET["name"])) {
if ($_GET["name"] == "tom"){
    echo "hello you are special";}
} else {
    echo "you are not special";
}

?>

</body>
</html> 
RussAwesome
  • 464
  • 5
  • 18
  • 1
    By definition of the error OP reports, the index value `name` has not been set. so your answer doesn't achieve anything. – Martin Aug 16 '16 at 12:00
0

for some reason i just tried it again with no changes to my code and it appears to be working i did not actually have to do anything.