0

i am new to learning PHP and i am trying to echo out the inputs in a html form.

this is the code

<?php
// Here is where your preprocessing code goes

// An example is already given to you for the First Name

    $fname = $_GET['fname'];
    $lname = $_GET['lname'];
    $email = $_GET['email'];
    $password = $_GET['password'];



?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Exercise 2 - GET Echo</title>
        <style>
        body {
            margin:0;
            padding:0;
            font-family: Arial;
        }

        form {
            margin:20px;
        }

        input[type="text"], input[type="password"] {
            width:150px;
            padding:3px;
            font-size:1em;
        }

        input[type="submit"] {
            padding:3px;
            font-size:1em;
        }

        label {
            display:inline-block;
            width:150px;
        }

        .input-container {
            padding:5px;
        }

        </style>        
    </head>
    <body>
        <form action="exercise-2.php" method="GET">
            <p><strong>Echo Form:</strong></p>
            <div class="input-container">
                <label for="fname">First Name</label>
                <input type="text" name="fname" value="" placeholder="First Name" id="fname">
            </div>
            <div class="input-container">
                <label for="lname">Last Name</label>
                <input type="text" name="lname" value="" placeholder="Last Name" id="lname">
            </div>
            <div class="input-container">
                <label for="email">Email Address</label>
                <input type="text" name="email" value="" placeholder="Email" id="email">
            </div>
            <div class="input-container">
                <label for="password">Password</label>
                <input type="text" name="password" value="" placeholder="Password" id="Password">
            </div>
            <div class="input-container">
                <input type="submit" value="Echo Values"/>
            </div>
        </form>
        <section>
            <h1>Echoed <code>GET</code> Values<h1>
            <p><strong>First Name:</strong> <?php if(isset($fname)){ 
    echo $fname;}
else
{
}   ?></p>
            <p><strong>Last Name:</strong> <?php if(isset($lname)){ 
    echo $lname;} ?></p>
            <p><strong>email:</strong> <?php if(isset($email)){ 
    echo $email;} ?></p>

    <p><strong>password:</strong> <?php if(isset($password)){ 
    echo $password;} ?></p>

        </section>
    </body>
</html>

the error i am getting is "Notice: Undefined index: fname in I:\twa\twa037\practicals\prac2\class 6\exercise-2.php" which repeats for each one of my variables

to my knowledge i believe it is because there is no input entered when the php is run. i have tried using the isset() function where if it is set to echo the value else do nothing.

  • Hi William, yes, you don't have the parameter $_GET['name'] – Maxi Schvindt Jan 04 '20 at 04:02
  • The warning is indicating that `fname` isn't present in the `$_GET` super global. If you are invoking PHP directly from the command line, you'll get that warning. For example, `echo $_GET['foo'];` will yield `PHP Notice: Undefined index: foo in php shell code on line 1`. – Erik Giberti Jan 04 '20 at 04:03
  • 1
    You are getting this notice because you are not checking if the $_GET variables have been passed before you assign them to your custom variables. – RyDog Jan 04 '20 at 04:04

0 Answers0