PHP $_GET will throw Undefined index

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

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.

there’s the coalesce operator

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

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

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

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

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

Hello, I tried POST but same error

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

1 Like

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.

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

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>

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

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

My bad. Forgot the quotes

$_POST['name']

Remove the action completely

 action="test9.php"

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

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

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

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>

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:

Sponsor our Newsletter | Privacy Policy | Terms of Service