Jump to content

Contact form


AnotherAndrew
 Share

Recommended Posts

I just realized that my contact form is not working since updating to the latest version of PW.

I gleaned the following code from the PW forums over a year ago. But I am a noob to php and can't seem to find what could be wrong with the code. Does anyone see anything obvious?

<?php

$sent = false;
$error = 'user@example.com';
$emailTo = 'user.bill@example.com'; // or pull from PW page field

// sanitize form values or create empty
$form = array(
    'fullname' => $sanitizer->text($input->post->fullname),
    'email' => $sanitizer->email($input->post->email),
    'comments' => $sanitizer->textarea($input->post->comments),
    ); 

// check if the form was submitted
if($input->post->submit) {
        
    // determine if any fields were ommitted or didn't validate
    foreach($form as $key => $value) {
        if(empty($value)) $error = "<p class='error'>Please check that you have completed all fields.</p>";
    }

    // if no errors, email the form results
    if(!$error) {
        $msg = "Full name: $form[fullname]\n" . 
               "Email: $form[email]\n" . 
               "Comments: $form[comments]"; 

        mail($emailTo, "Website contact form submission", "$form[comments]", "From: $form[email]");

        // populate body with success message, or pull it from another PW field
        $page->body = "<div id='message-success'><p>Thanks, your message has been sent.</p></div>"; 
        $sent = true;   
    }
}

if(!$sent) {

    // sanitize values for placement in markup
    foreach($form as $key => $value) {
        $form[$key] = htmlentities($value, ENT_QUOTES, "UTF-8"); 
    }

    // append form to body copy
    $page->body .= <<< _OUT

        <form action="./" method="post" id="contact-form">
			<fieldset>
				<legend>Send a note</legend>
					<ol>
						<li class="form-row">
							<span class="error" style="display: none;" ></span>
						</li>
						<li class="form-row">
							<label for="fullname">Name</label>
							<input id="fullname" name="fullname" type="text" size="30" class="name required default" title="Your name" value="$form[fullname]"/>
						</li>
						<li class="form-row">
							<label for="email">Email</label>
							<input id="inputemail" name="email" type="text" size="30" class="required email default" title="Your email address" value="$form[email]" />
						</li>
						<li class="form-row">
							<label for="comments">Message</label>
							<textarea name='comments' rows='5' cols='45' id='comments' title="Your message">$form[comments]</textarea>
						</li>
						<li class="form-row">
							<input type="submit" name="submit" value="Send" class="submit-button"/>
						</li>
					</ol>
			</fieldset>
        </form>

_OUT;

}
?><?php 

/**
 * Contact form template
 *
 */

include("./header.inc"); 
?>
        <div class="main-container">
            <div class="main wrapper clearfix">
                <article>
                    <section>
	                    <?php echo $page->body; ?>
                    </section>
                </article>
            </div> <!-- #main -->
        </div> <!-- #main-container -->

<?
include("./footer.inc"); 
  • Like 1
Link to comment
Share on other sites

Andrew, it would help greatly if you would let us know what is not working? What happens that you say it is not working? Errors? Have you enabled debug mode in /site/config.php to see if any warnings are shown?

Link to comment
Share on other sites

And I have figured it out.

From reviewing this thread where I originally got the code from Ryan, I realized that I was not supposed to be declaring a variable at the top. It needs to be

$error = '';

And I inadvertently seemed to have deleted the call to the variable here:

    $page->body .= <<< _OUT
    	$error
        <form action="./" method="post" id="contact-form">

Also, I couldn't see my problem locally since I'm using mamp and mamp has some issues with sending mail. Thanks everyone for taking a peek. Its been a long day!

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...