0

I'm converting a HTML site to WP for a client and they have a simple contact form in place.

If the submission is sucessful it currently, in it's HTML form, sends them to thankyou.html but I want to be able to send it to a custom permalink (Ie; to a page with the ID of 8) within the new installation, is it possible with the following code?

if (isset($_POST['submit']))
{
$to = "DESTINATION@EMAIL.COM";
$subject = "Web Site Form Enquiry";
$name_field = $_POST['contactname'];
$address = $_POST['address'];
$phone_field = $_POST['phone_no'];
$email_field = $_POST['email'];
$enquiry = $_POST['enquiry'];

$headers = "From: $email_field \r\n" . "X-Mailer: php";
$body = "From: $name_field\r\n Address: $address\r\n Telephone: $phone_field\r\n E-Mail: $email_field\r\n Enquiry:\r\n $enquiry\r\n";

if (mail($to, $subject, $body, $headers))
{
    // This needs to change to the permalink of a page with the ID of 8
        header('Location: ../../thankyou.php');
}
else 
{
    // This needs to change to the permalink of a page with the ID of 12
    header('Location: ../formerror.html');
}
}
2

If your form has been incorporated into WordPress as a page template, which I encourage, then get_permalink(8); // or 12 should do it.

If not, then http://example.com/?p=8 and http://example.com/?p=12 should always work. http://example.com/?page_id=8 and http://example.com/?page_id=12 uses the proper parameter for pages but ?p= works fine when I test it.

0

Try to set all you fields like this:

Step1.

//if form submitted
if(isset($_POST['submitted'])) {



// use this for all fields 
if(trim($_POST['contactname']) === '') {
        $nameError = 'Please enter your name.';
        $hasError = true;
    } else {
        $name = trim($_POST['contactName']);
    }


// this is for email
if(trim($_POST['email']) === '')  {
        $emailError = 'Please enter your email address.';
        $hasError = true;
    } else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
        $emailError = 'You entered an invalid email address.';
        $hasError = true;
    } else {
        $email = trim($_POST['email']);
    }

if(trim($_POST['message']) === '') {
        $messageError = 'Please enter a message.';
        $hasError = true;
    } else {
        if(function_exists('stripslashes')) {
            $message= stripslashes(trim($_POST['message']));
        } else {
            $message= trim($_POST['message']);
        }
    }

    if(!isset($hasError)) {
        $emailTo = get_option('tz_email');
        if (!isset($emailTo) || ($emailTo == '') ){
            $emailTo = get_option('admin_email');
        }
        $subject = '[PHP Snippets] From '.$name;
        $body = "Name: $name \n\nEmail: $email \n\nMessage: $message";
        $headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

        wp_mail($emailTo, $subject, $body, $headers);
        $emailSent = true;
    }
}
 ?>

Step2. Correct errors

<?php if(isset($emailSent) && $emailSent == true) { ?>
    <div class="thanks">
        <p>Thanks, your email was sent successfully.</p>
            </div>
        <?php } else { ?>
        <?php the_content(); ?>
            <?php if(isset($hasError) || isset($captchaError)) { ?>
                <p class="correct_errors">Please correct the highlighted errors and try again..<p>
            <?php } ?>

Step 3. Your form

<form action="<?php the_permalink(); ?>" id="contactform" method="post">

<p class="error_s"><?php if($nameError != '') { ?>
    <span class="error"><?=$nameError;?></span>
    <?php } ?></p>
    <p><label for="contactName" class="cContact">Name</label>&nbsp;&nbsp;<input type="text"  name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="required requiredField inputas" style="width:190px;" /></p>

....do the same for other inputs...

<p class="error_s">
<?php if($messageError != '') { ?>
<span class="error"><?=$messageError;?></span>
<?php } ?>
    </p>
<label for="message" class="cContact">Message</label>&nbsp;&nbsp;<textarea name="comments" id="message" class="requiredField" cols="48" rows="10" ><?php if(isset($_POST['message'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['message']); } else { echo $_POST['comments']; } } ?></textarea>

<input type="submit" name="submit" class="submitt" value="Send your message" style="cursor:pointer">
<input type="hidden" name="submitted" id="submitted" value="true" />

</form>

It's easy to do it even it looks different. All you need is to try more!

If still want to redirect to another page then use this:

if (mail($to, $subject, $body, $headers))
{
    // This needs to change to the permalink of a page with the ID of 8
         wp_redirect( 'http://yourdomain.com/succes/', 301 ); exit;
}
else 
{
    // This needs to change to the permalink of a page with the ID of 12
    wp_redirect( 'http://yourdomain.com/error/', 301 ); exit;
}
}
1
  • Thanks for this but unfortunately I can't change the form code. Had I done it myself originally I'd have done it in a very similar way, but my client (who is in turn doing this for another client) wants to keep everything as is Apr 21 '13 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.