Jump to content

Notice: Undefined variable


Deku219
 Share

Recommended Posts

all have the same errors: UNDEFINED VARIABLE....

HOW CAN I FIX IT??????

<!DOCTYPE html>

<?php
    define ("TITLE", "GET &amp; POST");
    
    if($_SERVER["REQUEST_METHOD"] == "POST"){
       $name = $email = "";
       $nameError= $emailError = "";
        
        if(!isset($_POST["post_name"])){
            $nameError = "Please Enter Your Name.";
           
        } else {
            $name = inputInfo($_POST["post_name"]); 
        }
        
        if(!isset($_POST["post_email"])){
            $emailError = "Please Enter Your Email.";
        } else {
            $email = inputInfo($_POST["post_email"]);
        }
    }

    function inputInfo($data){
        $data = trim(stripslashes(htmlspecialchars($data)));
        return $data;
    }
?>

<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE-Edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>PHP => Loops</title>
        <link href="css/bootstrap.min.css" rel="stylesheet">
    </head>
    
    <body>
        <div class="container">
            <h1><?php echo TITLE?></h1>
            
            <h4>Submitted to current page</h4>
            <p class="text-danger">* Required Field</p>
            <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
            <!-- this line -->    <small class="text-danger">* <?php echo $nameError ?></small>
                <input type="text" placeholder="Name" name="post_name" class="form-control col-sm-6 offset-3" value="<?php echo $name; ?>"><br>
              <!-- this line -->   <small class="text-danger">* <?php echo $emailError; ?></small>
                <input type="email" placeholder="Email" name="post_email"  class="form-control col-sm-6 offset-3" value="<?php echo $email; ?>"><br>
                <input type="submit" value="submit" name="post_submit" class="btn btn-success offset-8">
            </form>
      
            <?php
                echo "<h4>Your Info:</h4>";
            <!-- this line --> echo "$name <br> $email <br>";
            ?>
            
        </div>
        
        <script src="js/jquery.min.js"></script>
        <script src="js/bootstrap.bundle.min.js"></script>
    </body>
</html>

Thanks !

Link to comment
Share on other sites

On 4/2/2019 at 3:07 PM, Deku219 said:

<!-- this line --> echo "$name <br> $email <br>";

You have an HTML comment inside a PHP code block.

 

On 4/2/2019 at 3:07 PM, Deku219 said:

<?php echo $nameError ?></small>

You're missing a semicolon here, but I'm not sure if that makes a difference.

 

Can you copy and paste the error messages directly?

 

Here's a shorthand for <?php echo $variable_to_output ;?>. It might be a little faster to write.

<?= $variable_to_output; ?>

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...