-1

I am trying to learn PHP.I can't run this example.But I think codes are true.I am trying it at my localhost.How can i run it?

<html>
<head>
<meta http-equiv="Content-Type" content="text/HTML; charset=utf-8" />
<title>My Page</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name"fname">
<input type="submit">
</form>
<?php
$name=$_REQUEST['fname'];
echo $name;
?>
</body>
</html>

Error: ( ! ) Notice: Undefined index: fname in C:\wamp\www\index.php on line 12

Soykan
  • 63
  • 6

3 Answers3

1

You haven't submitted the form yet so $_POST['fname'] does not exist. Try this:

<?php
// turns off all errors and notices, recommended for a production website
// comment out this code if on development environment, it will make you a better programmer
error_reporting(0);
?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/HTML; charset=utf-8" />
        <title>My Page</title>
    </head>
    <body>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
            Name: <input type="text" name"fname">
            <input type="submit">
        </form>
        <?php
        if(isset($_REQUEST['fname']))
        {
            echo $_REQUEST['fname'];
        }
        ?>
    </body>
</html>
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
0
<html>
<head>
<meta http-equiv="Content-Type" content="text/HTML; charset=utf-8" />
<title>My Page</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
if(isset($_POST['fname'])){
$name=$_POST['fname'];
echo $name;
}
?>
</body>
</html>
0

You need to check if the variable is set, otherwise you will end up with that error message. Do something like:

<html>

    <head>
        <meta http-equiv="Content-Type" content="text/HTML; charset=utf-8" />
        <title>My Page</title>
    </head>

    <body>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">Name:
            <input type="text" name "fname">
            <input type="submit">
        </form>
        <?php 
             if (isset($_REQUEST['fname'])) {
                 $name = $_REQUEST[ 'fname']; 
                 echo $name; 
             }
        ?>
    </body>

</html>
putvande
  • 15,068
  • 3
  • 34
  • 50