help with php email form

Hi there, I hope I’ve used the tags right on the site to display this. I’m trying to create an email form, but my error page isn’t working. Hope someone can help, thanks

[php]<?php

// Information to be modified

if (isset($_POST[“recipient”])) {
$rec = $_POST[“recipient”];
}
if ($rec == “Jim”) {
$your_email="[email protected]";
}
elseif ($rec == “Angie”) {
$your_email="[email protected]";
}
else {
$your_email="[email protected]";
}

$subject = “Message from WEBSITE”; // subject of the email that is sent
$thanks_page = “thankyou.html”; // path to the thank you page following successful form submission

//

if (isset($_POST[“submit”])) {
$nam = $_POST[“name”];
$ema = trim($_POST[“email”]);
$com = $_POST[“comments”];
$loadtime = $_POST[“loadtime”];

if (get_magic_quotes_gpc()) { 
$nam = stripslashes($nam);
$ema = stripslashes($ema);
$com = stripslashes($com);
}

$error_msg=array();

if (empty($nam) || !preg_match("/^[\s.’-\pL]{1,60}$/u", $nam)) {
$error_msg[] = “The name field must contain only letters, spaces and basic punctuation (. - ')”;
}

if (empty($ema) || !filter_var($ema, FILTER_VALIDATE_EMAIL)) {
$error_msg[] = “Your email must have a valid format, such as [email protected]”;
}

$limit = 1000;

if (empty($com) || !preg_match("/^[0-9/-\s’()!?.,:;\pL]+$/u", $com) || (strlen($com) > $limit)) {
$error_msg[] = “The Comments field must contain only letters, digits, spaces and basic punctuation ( ’ - , . : ; / and parentheses), and has a limit of 1000 characters”;
}

$totaltime = time() - $loadtime;

if($totaltime < 7) {
echo(“Please fill in the form before submitting!”);
exit;
}

// Assuming there’s an error, refresh the page with error list and repeat the form

if ($error_msg) {
echo ’

Error logo
Home | Services | Gallery | Testimonials | Contact Us
	<div class="content">
	<h1>Oh dear!</h1>
	<p>Unfortunately, your message could not be sent. The form as you filled it out is displayed below. Make sure each field completed, and please also address any issues listed below:</p>
	<ul class="err">';

foreach ($error_msg as $err) {
echo ‘

  • ’.$err.’
  • ’;
    }
    echo ’


    Name
    <input name=“name” type=“text” size=“40” maxlength=“60” id=“name” value="’; if (isset($_POST[“name”])) {echo $nam;}; echo ‘">


    Email Address
    <input name=“email” type=“email” size=“40” maxlength=“60” id=“email” value="’; if (isset($_POST[“email”])) {echo $ema;}; echo '">


    Comments
    '; if (isset($_POST[“comments”])) {echo $com;}; echo ’





    van
    '; exit(); }

    $email_body =

    "Name of sender: $nam\n\n" .
    "Email of sender: $ema\n\n" .
    "COMMENTS:\n\n" .
    "$com" ; 
    

    // Assuming there’s no error, send the email and redirect to Thank You page

    if (!$error_msg) {
    mail ($your_email, $subject, $email_body, “From: JAF-Waste noreply@WEBSITE\r\n”. “Reply-To: $nam <$ema>” . “Content-Type: text/plain; charset=utf-8”);

    header (“Location: $thanks_page”);
    exit();
    }

    }
    ?>

    WEBSITE logo
    Home | Services | Gallery | Testimonials | Contact Us

    Contact details

     
    JIM ANGIE

    PHONE NUMBER
    [email protected]

    PHONE NUMBER
    [email protected]

    Alternatively you can use the form below

    Choose a Recipient <?php if (isset($_POST['recipient'])) {print "$rec";} else {print "Please choose a Recipient";}?> JIM ANGIE
    	<div>
    		<label for="name">Name</label>
    		<input name="name" type="text" id="name" placeholder="your name" value="<?php if (isset($_POST["name"])) {echo $nam;} ?>">
    	</div>
    	<div>
    		<label for="email">Email Address</label>
    		<input name="email" type="email" id="email" placeholder="your email address" value="<?php if (isset($_POST["email"])) {echo $ema;} ?>">
    	</div>
    	
    	<div>
    		<label for="comm">Comments</label>
    		<textarea name="comments" rows="5" id="comm"><?php if (isset($_POST["comments"])) {echo $com;} ?></textarea>
    	</div>
    	
    	<input type="hidden" name="loadtime" value="<?php echo time(); ?>">
    	
    	<div>
    		<input type="submit" name="submit" value="Send">
    	</div>
    </form>
    
    van
    [/php] [code] .content { width: 100%; margin: 0 auto; } .err { color: red; font-size: 0.875em; margin: 1em 0; padding: 0 2em; } label { margin-bottom: 2px; }

    input[type=“text”], input[type=“email”], textarea {
    font-size: 0.75em; width: 98%; font-family: arial; border: 1px solid #ebebeb; padding: 4px; display: block;
    }
    input[type=“radio”] {
    margin: 0 5px 0 0;
    }
    textarea {
    overflow: auto;
    }

    #contact{
    margin: 0 0 10px 0; ;
    }

    #banner {
    position:absolute;
    left: 0px;
    top: 0px;
    background-color: #d6d6d6;
    border-bottom: 2px solid #434A57;
    height: 110px;
    width: 100%;
    z-index: 1;

    }
    #holder {
    top: 111px;
    left: 0px;
    width: 100%;
    height:22px;
    position: absolute;
    border-bottom: 2px solid #434A57;
    z-index: 2;

    }
    #links {
    top: 0px;
    left: 10px;
    position: absolute;
    z-index: 1;

    }
    a {
    font-family:“Lucida Sans Unicode”, “Lucida Grande”, sans-serif;
    font-size:medium;
    width: 500px;
    height: 22px;
    color: #434A57;
    }

    #contactus {
    top: 120px;
    left: 100px;
    position: absolute;
    font-family:"Lucida Sans Unicode", "Lucida Grande", sans-serif;
    font-size:medium;
    width: 500px;
    height: 500px;
    z-index: 1;
    color: #434A57;
    

    }

    @media (max-width: 1000px) {
    .van {
    display: none; }
    }

    #van {
    position: absolute;
    right:0px;
    bottom: 0px;		
    

    }

    [/code]

    [php] if (get_magic_quotes_gpc()) {
    $nam = stripslashes($nam);
    $ema = stripslashes($ema);
    $com = stripslashes($com);
    }[/php]
    This is a waste of script and space, because ->
    5.4.0 Always returns FALSE because the magic quotes feature was removed from PHP. http://php.net/manual/en/function.get-magic-quotes-gpc.php

    I personally would consider using a third party mailer - I use PHPMailer https://github.com/PHPMailer/PHPMailer

    It’s easy to implement and is more secure than something you would write yourself. Why reinvent the wheel? :wink:

    P.S. You still need to validate the user’s input.

    Well, yes, PHPmailer is a great library and it is suggested here all of the time. You may want to look into it for
    a future upgrade. It has error-handling that the PHP mail() function does not. (Mail() only says it sent the email
    or not. No further info…)

    Now, more info for you… When you handle forms, normally, you would have PHP code at the top to read the
    input fields, validate them, process your data with an email and maybe log to your system logs or update your
    database info. Usually at that point, you have it post back to the same page placing the data the user entered
    into the current form in case they wanted to alter it and resend it. During the validation section, you would create
    any error messages and just display that on the live form page near the top, usually in a different color that will
    make the user understand the problem. It is a huge waste of code to rewrite the entire page with errors shown
    in it. No need to do that. To create and show the the errors, each programmer has their own way to handle
    the code. I just store them in a variable and display it if the inputs fail. Something loosely like this:
    $error_message = “”;
    if(some condition){ $error_message .= “
    You did not enter your name!”; }
    if(some condition){ $error_message .= “
    Special characters not allowed in your name!”; }
    And, in the form, just above the fields to be entered, I use an optional display based on the errors if they exist:

    <?PHP // If there is an error message from posted info, show it here... if ($error_message != "") { echo '
    '; echo 'Contact Form Error!
    ' . $error_message; echo '
    '; } ?>

    Just off the top of my mind, not live code. But, as you see, no need to retype the entire page in code. Of
    course, you would also check for errors before sending an email or updating your logs or database. There are
    many other ways to keep track of errors such as storing them into an array, but, this is very simple and works
    well for beginners.

    Sponsor our Newsletter | Privacy Policy | Terms of Service