Email Contact Form query

  • 15 years ago
    I have a simple contact form which I want to use to send an email from my website.  It all appears to work OK, however, for some reason whatever text is used on the submit button label is being appended to the end of the message field.

    The fields on my form are:  req_name, req_email, req_subject and req_message.

    The email that is sent contains the information as expected, the only problem is that at the end of the message the text from the submit button label is also included.

    I am very new to PHP (and web development in general) so any ideas would be greatly appreciated.  My code was obtained from a college course I attended and I am adapting it to my needs (I do not claim to understand the whole script):

    <?

    // Recipient of message (This can be changed via the form itself)
    $recipient .= '[email protected]';

    // Subject of message (This can be changed via the form itself)
    $subject = $req_subject;

    // This is a list of domains that can run EZ FormMail. Do not include
    // www, just the actual domain/ip address!
    $referers = array('website.com');

    // This is the page that users will be redirected to after the form is
    // processed successfully. You will need to create the page
    $success_url = 'http://www.website.com/contactform_OK.htm';

    // Your site URL
    $siteurl = 'http://www.website.com';

    ###########################################################
    # DO NOT EDIT BELOW THIS LINE                             #
    ###########################################################

    function Print_Footer() {
       echo ', to return to the previous page.';
    }
    // This part tells the script where to accept form data from. It specifies
    // the domain name and rejects the information if it's from another or
    // unauthorised source.

    function Check_Referer() {
       global $referers;
       $temp = explode('/', $_SERVER['HTTP_REFERER']);
       $referer = $temp[2];
       $found = false;
       foreach ($referers as $domain) {
           if (stristr($referer, $domain)) { $found = true; }
       }
       return $found;
    }
    // This part tells the user that what they are doing is not allowed
    if ($_POST) {
       if (Check_Referer() == false) {
           echo '<font size="+1" color="#FF0000">Error: Invalid Referer</font><BR>';
           echo 'You are accessing this script from an unauthorized domain!';
           Print_Footer();
           die();
       }
       $ctr = 0;
       
       $isrealname = 0;
       $isemail = 0;

    // This section valiodates part of the form. This eliminates the need for javaScript
    // within the environment. You need to call the HTML equivalents exactly the same as
    // they are in line 60 below.

       foreach ($_POST as $key => $val) {
           if ($key == 'req_name') { $isrealname = 1; }
           if ($key == 'req_email') { $isemail = 1; }
           if (substr($key, 0, 4) == 'req_' || $key == 'req_name' || $key == 'req_email'|| $key == 'req_subject' || $key == 'req_message') {
               if ($val == '') {
                   if ($ctr == 0) {
                       echo '<font size="+1" color="#FF0000">Error: Missing Field(s)</font><BR>';
                       echo 'The following <i>required</i> field(s) were not filled out:<BR>';
                   }
                   echo '<BR>- <b>'.substr($key, 4).'</b>';
                   $ctr++;
               }
           }
       }
       if ($ctr > 0) {
           echo '<p>Click <a href="javascript:history.go(-1)">here</a> to go back';
           Print_Footer();
           die();
       }
       else {
           if ($isrealname == 0) {
               echo '<font size="+1" color="#FF0000">Error: Missing Field</font><BR>';
               echo 'No "req_name" field found.<p>Please click <a href="'.$siteurl.'">here</a> to return to the home page.';
               Print_Footer();
               die();
           }
           elseif ($isemail == 0) {
               echo '<font size="+1" color="#FF0000">Error: Missing Field</font><BR>';
               echo 'No "req_email" field found.<p>Please click <a href="'.$siteurl.'">here</a> to return to the home page.';
               Print_Footer();
               die();
           }
       }
       
       if (!(preg_match("/^.{2,}?@.{2,}\./", $_POST['req_email']))) {
               echo '<font size="+1" color="#FF0000">Error: Invalid E-mail</font><BR>';
               echo 'The e-mail address you entered (<i>'.$_POST['email'].'</i> is invalid.';
               Print_Footer();
               die();
       }

    // The next section describes the email that will be sent.

       $body = "Below is a message sent via the Contact Us form. It was submitted on ".date('l, F jS, Y').' at '.date('g:ia').":\n";
       
       foreach ($_POST as $key => $val) {
           if ($key == 'recipient') { $recipient = $val; }
           elseif ($key == 'req_subject') { $subject = $val; }
           //elseif ($key == 'req_subject') { $subject = 'req_subject'; }
           else {
               if ($key != 'req_name' && $key != 'req_email') {
    //                $body .= "\n".str_replace('req_', '', $key).": $val";
                   $body .= "\n$val";
               }
           }
       }
       
       
       $mailheaders = "From: ".$_POST['req_name']." <".$_POST['req_email'].">\n";
           
       mail($recipient, $subject, $body, $mailheaders);
       header("Location: $success_url");
    }
    else {
       echo '<center>You have accessed this page from an invalid location. Please click <a href="'.$siteurl.'">here</a> to go to '.$siteurl.'.</center>';
    }
    Print_Footer();
    ?>

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“In theory, theory and practice are the same. In practice, they're not.”