PHP $_GET will throw Undefined index

#1

Hi guys, this might seem very simple but I don’t know where is the issue, please check the code:

    <!DOCTYPE html>
<html>
<head>
	<title>Test</title>
</head>
<body>
	<form action="test9.php" method="get">
		Name:<input type="text" name="Name">

		Age:<input type="text" name="Age">
		<input type="submit">
	</form>
	<br>
	Your Name is :<?php echo $_GET["Name"] ?>
	<br>
	Your Age is: <?php echo $_GET["Age"] ?>
</body>
</html>

Error:

Your Name is :
Notice : Undefined index: Name in C:\laragon\www\test\test9.php on line 14

Your Age is:
Notice : Undefined index: Age in C:\laragon\www\test\test9.php on line 16

#2

As long as you access the file with the params it should be fine (apart from no escaping on output which will make your users vulnerable to XSS)
yourfile.php?Name=foo&age=20

If you access the file without the params initially then you need to set the params to an initial value in the code or they (quite correctly) aren’t available to output.

#3

there’s the coalesce operator

https://wiki.php.net/rfc/null_coalesce_equal_operator

#4

Thank you @JimL for your answer, if you would to fix it then how can this be fixed?

#5

you should have enough info to fix this now ^^ if you face any problems please return with any new code/errors you have.

#6

You should be using POST. You are not “getting” anything from the server.

#7

Well your answer didn’t give me any clue, how am I suppose to learn if you don’t show real practical help, unless you don’t know how to fix it yourself as well, which put us in the same boat

#8

Hello, I tried POST but same error

#9

Yeah no that’s not going to work on me. Good luck

1 Like
#10

It’s ok, you don’t have to contribute anymore, you are not giving me oxygen to live, and I’m finding the answer sooner or later, but this only shows what type a person is and how decent a real helper can be.

#11

The error is because you are trying to use a variable that does not exist. empty is your friend here.

#12

I understand, the funny thing is that I’m following other person’s example and my code is identical to what I provided here and it doesn’t show any error for him.

Now you said that the variable do not exist and they are empty, well I tried to add them and give even a hard-coded value and still I’m getting the issue, here is the code:

  <!DOCTYPE html>
<html>
<head>
	<title>Test</title>
</head>
<body>
	<form action="test9.php" method="get">
		Name:<input type="text" name="Name">

		Age:<input type="text" name="Age">
		<input type="submit">
	</form>
	<br>
	Your Name is :<?php $Name = "Jack"; echo $_GET["Name"] ?>
		<br>
		Your Age is:<?php $Age = 4; echo $_GET["Age"]?>
</body>
</html>
#13

The “other guy” doesnt have error reporting turned on. If he did, he would have the same problem. Find another tutorial. He doesn’t know what he is doing.

Use POST and lowercase naming.

Your Name is : <?= !empty($_POST['name']) ? htmlspecialchars($_POST['name'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') : ''; ?>

EDIT: Added missing quotes

#14

Thank you, however I’m getting a new issue, I minimized the code to only “name”

  <!DOCTYPE html>
<html>
<head>
	<title>Test</title>
</head>
<body>
	<form action="test9.php" method="post">
		Name:<input type="text" name="name">

		<input type="submit">
	</form>
	<br>


Your Name is : <?=!empty($_POST[name]) ? htmlspecialchars($_POST[name], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') : '';?>


</body>
</html>

Warning : Use of undefined constant name - assumed ‘name’ (this will throw an Error in a future version of PHP) in C:\laragon\www\test\test9.php on line 15

#15

My bad. Forgot the quotes

$_POST['name']

Remove the action completely

 action="test9.php"
#16

Excellent, thank you, however the code will not work without the <form action="test9.php" method="post"> If I remove it, it won’t display the name I entered when I hit submit.

ADMIN EDITED

#17

Post your entire current code. If it is not working without the action, then something is wrong.

#20

Here is the full working code:

<!DOCTYPE html>
<html>
<head>
	<title>Test</title>
</head>
<body>
	<form action="test9.php" method="post">
		Name:<input type="text" name="name">

		<input type="submit">
	</form>
	<br>


Your Name is : <?=!empty($_POST['name']) ? htmlspecialchars($_POST['name'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') : '';?>


</body>
</html>

if you got the chance to try it later on, you will notice that if you removed the action test9.php then when you hit submit it won’t return it unless I’m missing something else

#23

I deleted the action and it works fine.

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
</head>
<body>
    <form method="post">
        Name:<input type="text" name="name">

        <input type="submit">
    </form>
    <br>


Your Name is : <?=!empty($_POST['name']) ? htmlspecialchars($_POST['name'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') : '';?>


</body>
</html>
#24

Oww, sorry, yeah, I figured it, when you said remove the action, I removed the method as well.

Thank you and God bless you :):smile: