Click here to Skip to main content
15,920,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting error in this very simple program

HTML
<form action ="index.php" method="GET">

Name:<br> <input type="text" name="name"><br>
    Age:<br> <input type="text" name="age" size="5"><br><br>
   <input type="submit" value="submit">
</form>

<?php
$name = $_GET['name'];
$age = $_GET['age'];
echo 'I am '.$name.'and I am '.$age;
?>



I get this error

The form gets displayed. But underneath I see this error

Notice: Undefined index: name in C:\xampp\htdocs\start\another.php on line 11

Notice: Undefined index: age in C:\xampp\htdocs\start\another.php on line 12
Posted
Comments
Sergey Alexandrovich Kryukov 18-Nov-15 14:59pm    
If "index.php" the same file as the one you show here?
—SA

1 solution

First of all, it's not clear why you use method="GET" and $_GET; by your semantic, it should rather be method="POST" and $_POST.
But this is not a problem. The problem is different.

Please see my comment to the question. It looks like you show fragment of one single file. If this file was not "index.php", it would not work at all. But if it, it would not work when the page is just loaded. At this moment, the HTTP request with name and age is not yet sent, so the indices are not yet defined. When you submit the form, the HTTP request will come with these two indices, so your script will receive them as values by defined indices.

The simplest thing you can do is the check-up of the index in $_GET or $_POST. This is one of the ways to do so: http://php.net/manual/en/function.array-key-exists.php[^].

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900