0

Before You read the code, I have tried separating each part into their own php files and just using requires to fetch the code, but using requires or having all the code in the same file I seem to be getting the same errors regardless. I think it may have something to do with the the version of PHP I'm using.

I seem to be getting an error with submit on line 3 of the BACKEND part. Being an undefined property.

The second is an undefined error on the USER FEEDBACK section.

I've used this template before and has worked successfully.

I'm running PHP 5.4.12 and Apache 2.4.4 using WAMP on my Windows 8.1 Pro PC.

Any help would be appreciated

/** BACKEND **/

<?php
if($_POST['submit'])
{
    $fName=$_POST['fName'];
    $topic=$_POST['topic'];
    $email=$_POST['email'];
    $message=$_POST['message'];

    function verify_email($email)
        {
             if(!preg_match('/^[_A-z0-9-]+((\.|\+)[_A-z0-9-]+)*@[A-z0-9-]+(\.[A-z0-9-]+)*(\.[A-z]{2,4})$/',$email))
             {
            return false;
             }
             else
             {
                 return $email;
             }
    }

    function verify_email_dns($email)
        {
            list($name, $domain) = split('@',$email);

            if(!checkdnsrr($domain,'MX'))
            {
                return false;
            }

            else
            {
                return $email;
            }
    }

    if(verify_email($email))
        {
        if(verify_email_dns($email))
            {
            if ($fName=='')
                {
            header('location:./contact.php?error=missing');
            }
                elseif ($email=='')
                {
                header('location:./contact.php?error=missing');
        }
                elseif ($message=='')
                {
            header('location:./contact.php?error=missing');
        }
                else
                {
            foreach ($myvars as $var)
                    {
                if (isset($_POST[$var]))
                        {
                    $var=$_POST[$var];
                }
            }

            $subject = "Email Submission for review"; 
                    $add.="test.email@gmail.com"; 
                $msg.="First Name:          \t$fName\n";
            $msg.="Email:               \t$email\n";
            $msg.="Topic:               \t$topic\n";
            $msg.="Message:             \t$message\n";
            $mailheaders="From: $email\n"; 
                $mailheaders.="Reply-To: $email\n"; 
            mail("$add", "$subject", $msg, $mailheaders);
            header('location:./contact.php?error=none');
        }//end else  
        }//end inner-if
            else
            {
            header('location:./contact.php?error=mx');
            }
    }// end outter-if

        else
        {
        header('location:./contact.php?error=format');
    }
    }// end starting if





    /** VIEW for form **/
 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="contactForm"> 
     <label for="fName" class="first-name">Name:</label>
 <input type="text"  name="fName" value="" id="fName">
 <br><br>

 <label for="email" class="email-name">Email:</label>
 <input type="text"  name="email" value="" id="email">
 <br><br>

 <label for="topic" class="subject-name">Subject:</label>
 <input type="text"  name="topic" value="" id="topicsubject">
 <br><br>

 <label for="message" class="message-name">Message:</label>
 <textarea name="message"  rows="5" cols="60" id="message"></textarea>
 <br><br>

<input type="submit" name="submit" id="submit-btn"  class="submit-btn" value="Email Me">
 </form>





    /** USER FEEDBACK if error occurs **/

<?php
    $error=$_GET['error'];

        switch ($error) 
        {

            case "mx":
        echo "<br><span class='red'>Your email address you entered is invalid.  Please try again.</span><br>";
        break;

        case "format":
        echo "<br><span class='red'>Your email address is not in the correct format, it should look like name@domain.com. Please try again.</span><br>";
        break;

        case "missing":
        echo "<br><span class='red'>You seem to be missing a required field, please try again.</span><br>";
        break;

    case "none":
        echo "<br>Your email was sent.  I will get back to you as soon as I can.  Thank you for your interest.<br>";
    break;

    default:
        echo "<br><br>";
        }
?>
nickhiebertdev
  • 525
  • 1
  • 6
  • 21
  • In order to avoid undefined offset errors, try checking for the existence of an offset before trying to access it. Example here: [PHP Notice Undefined Offset](http://stackoverflow.com/questions/22205690/php-notice-undefined-offset#answer-22205843) – showdev Jun 03 '14 at 20:25

1 Answers1

1

You are assuming that there are POST and GET variables when you are visiting the page. So its possible that $_POST['submit'] only exists when you actually submit the form otherwise you will get an error when first visiting that page.

try this condition instead:

if(isset($_POST['submit']) ) {
    // now its safe to do something
}

You should never assume that any $_POST or $_GET variable is available when visiting the page.

Also off topic: In your HTML you are using an 'action' attribute with the same url as the page you are visiting on this line here:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="contactForm">

Basically if you just leave out the action attribute all together it will have the same effect and its also semantic to do so. This is a better way of doing it and it has the same effect:

<form method="post" name="contactForm">

You can check this previous Stack Overflow question for a better explanation on that matter:

Is it a good practice to use an empty URL for a HTML form's action attribute? (action="")

Community
  • 1
  • 1
JoeMoe1984
  • 1,912
  • 3
  • 21
  • 31