0

I keep getting Undefined variable on lines 52, 53, 54, 55 and have no idea on how fix it. Any help would be greatly appreciated.

<table>
            <form name="emailMe" id="emailMe" action="contactMe.php" method="post">

                <tr><th><p>First Name </p></th> <td><p><input type="text" name="firstName" maxlength="10"value="<?php print($fName); ?>" /></p></td></tr>
                <tr><th><p>Last Name </p></th> <td><p><input type="text" name="lastName" maxlength="25"value="<?php print($lName); ?>" /></p></td></tr>
                <tr><th><p>Email </p></th> <td><p><input type="text" name="email" maxlength="50"value="<?php print($email); ?>" /></p></td></tr>
                <tr><th><p>Message </p></th> <td><p><input type="text" name="message" maxlength="250"value="<?php print($message); ?>" /></p></td></tr>
        </table>

                <p><input type="submit" name="submit" value="Submit" />
                <input type="reset" value="Clear Form" /></p>
            </form>

        <?php 

            if(isset($_POST["submit"])){

            $errorCount = 0;
            $fName      = $_POST['firstName'];
            $lName      = $_POST['lastName'];
            $email      = $_POST['email'];
            $message    = $_POST['message'];
mvmrocks
  • 85
  • 2
  • 11

5 Answers5

3
<?php print($fName); ?>
<?php print($lName); ?> 
<?php print($email); ?> 
<?php print($message);?>

All such variables in your HTML code have not been declared before you are using them. That's causing the error. None of those variables have been initialized before you print them. You could put PHP code before your HTML to initialize them. Like this example

if(isset($_POST["submit"]))
{
 $fName = $_POST['firstName'];
}
else
{
 $fName = "";
}

Edit:

Like this

<?php 

        if(isset($_POST["submit"])){
        $errorCount = 0;
        $fName      = $_POST['firstName'];
        $lName      = $_POST['lastName'];
        $email      = $_POST['email'];
        $message    = $_POST['message'];
        }
        else
        {
        $fName      = "";
        $lName      = "";
        $email      = "";
        $message    = "";
        }
?>

And this needs to go Above your HTML

Hanky Panky
  • 44,997
  • 8
  • 67
  • 93
  • I try to declare them above the form but id didn't seem to fix the problem. – mvmrocks Feb 08 '13 at 06:06
  • Yes because your code only declares them when $_POST is set but your html wants to use them always even without $_POST. So first you have to declare them above html and secondly you have to initialize them with maybe blank values even if $_POST is not set – Hanky Panky Feb 08 '13 at 06:10
1

Follow this simple step:
Change from this:
<tr><th><p>First Name </p></th> <td><p><input type="text" name="firstName" maxlength="10" value="<?php print($fName); ?>" /></p></td></tr>

To this:
<tr><th><p>First Name </p></th> <td><p><input type="text" name="firstName" maxlength="10" value="<?php if(isset($fname)) print($fName); ?>" /></p></td></tr>

Repeat like this in all remaining steps & you are good to go.

007mrviper
  • 376
  • 3
  • 19
0

hey declare the <table> inside <form> tag like this:

 <form name="emailMe" id="emailMe" action="contactMe.php" method="post">
<table>
                <tr><th><p>First Name </p></th> <td><p><input type="text" name="firstName" maxlength="10"value="<?php print($fName); ?>" /></p></td></tr>
                <tr><th><p>Last Name </p></th> <td><p><input type="text" name="lastName" maxlength="25"value="<?php print($lName); ?>" /></p></td></tr>
                <tr><th><p>Email </p></th> <td><p><input type="text" name="email" maxlength="50"value="<?php print($email); ?>" /></p></td></tr>
                <tr><th><p>Message </p></th> <td><p><input type="text" name="message" maxlength="250"value="<?php print($message); ?>" /></p></td></tr>
        </table>

                <p><input type="submit" name="submit" value="Submit" />
                <input type="reset" value="Clear Form" /></p>
            </form>
KAsh
  • 194
  • 3
  • 21
0

You are using variables before declaring them. Change as below.

    <?php 

                if(isset($_POST["submit"])){

                $errorCount = 0;
                $fName      = $_POST['firstName'];
                $lName      = $_POST['lastName'];
                $email      = $_POST['email'];
                $message    = $_POST['message'];

    ?>
<form name="emailMe" id="emailMe" action="contactMe.php" method="post">
    <table>
            <tr><th><p>First Name </p></th> <td><p><input type="text" name="firstName" maxlength="10"value="<?php print($fName); ?>" /></p></td></tr>
                    <tr><th><p>Last Name </p></th> <td><p><input type="text" name="lastName" maxlength="25"value="<?php print($lName); ?>" /></p></td></tr>
                    <tr><th><p>Email </p></th> <td><p><input type="text" name="email" maxlength="50"value="<?php print($email); ?>" /></p></td></tr>
                    <tr><th><p>Message </p></th> <td><p><input type="text" name="message" maxlength="250"value="<?php print($message); ?>" /></p></td></tr>
            </table>
            <p><input type="submit" name="submit" value="Submit" />
            <input type="reset" value="Clear Form" /></p>
</form>

NB:I can see you checking a $_POST["submit"] in your form, If it is the same submit you are using in the HTML code, you will get the error even after changing it to my code.

Because, in that case, you are using variables that are not defined, since the form is not submitted initially.

Vishnu R
  • 1,811
  • 3
  • 23
  • 44
0

Please remove following line from current position and put those line before the table tag.

        if(isset($_POST["submit"])){

        $errorCount = 0;
        $fName      = $_POST['firstName'];
        $lName      = $_POST['lastName'];
        $email      = $_POST['email'];
        $message    = $_POST['message'];