Simple error but no idea why when practising 'Get'

Hi guys

I’m practising doing a “get” with a simple HTML5 form.

Although it does what I want it to do, everytime I load it to test out in a browser I keep getting this error and I’ve no idea why…

This is the error

( ! ) Notice: Undefined index: myname in C:\\wamp\\www\\userlogin\\get1.php on line 19

Call Stack
#	Time	Memory	Function	Location
1	0.0030	363520	{main}( )	..\\get1.php:0


Here is my code

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>get1.php</title>
</head>

<body>
<form action="get1.php" method="get">
<input type="text" name="myname"> <br>
<input type="submit" value="Type your name to see how old you are!">

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

<?php

$name = $_GET["myname"];

if($name)
	echo "Nong " . $name.  " is 24 years old";


//review this 

?>

Any ideas…?

Why not use post?

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>get1.php</title>
</head>

<body>
<form action="get1.php" method="post">
<input type="text" name="myname"> <br>
<input type="submit" name='submit' value="Type your name to see how old you are!">

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

<?php

if(isset($_POST['submit']) :
$name = $_POST["myname"]; // Remember to clean your inputs

if($name != '') :
echo "Nong " . $name. " is 24 years old";
endif;
endif;
?>

Good luck

Thanks for the code dude, I must be doing something wrong as Im getting a syntax error on your code too, perhaps I havent configured something correctly

Im in the process of learning PHP so am just practising writing different scripts/programs at the moment. In fact “post” was the next one lol

Any ideas why Im getting this error on the ‘get’ variable…?
Ive checked everything…

Can you post the error?

Because the value is not set, and myname is null. And that is just a notice, not a critical error.

Ah yes I got it!

The error I was getting from your code was because I didnt change the form action to match the name of my page. Also on line 19 there was a closing bracket missing.

Thanks so much for helping me man.

The “get” error is one of complete mystery and by the sounds of it get isnt a great idea to use anyway

Ah… that silly brackets… :rolleyes:

if(isset($_POST['submit'])) :

Gets me everytime :slight_smile:


<?php
#set a default
$name = null;

#check if a new name has been sent
if(false === empty($_GET['name'])){
  $name = $_GET['name'];
}
?>
<!DOCTYPE HTML>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>get1.php</title>
  </head>
  <body>
    
    <?php if(false === empty($name)): ?>
      <p>You previously entered: <?php echo $name; ?></p>
    <?php endif; ?>
    
    <form action="get1.php" method="get">
      <input type="text" name="name" /> <br />
      <input type="submit" name="submit" value="Type your name to see how old you are!" />
    </form>
  </body>
</html>

Cheers man, day off today so gonna crack on with the code again tommorow

Thanks again