如何将此联系人表重定向到特定的固定链接

时间:2013-04-21 作者:Dean Elliott

我正在为客户将HTML站点转换为WP,他们有一个简单的联系表单。

如果提交成功,则当前以HTML格式将其发送给thankyou。html,但我希望能够将其发送到新安装中的自定义永久链接(即,发送到ID为8的页面),是否可以使用以下代码?

if (isset($_POST[\'submit\']))
{
$to = "[email protected]";
$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 个回复
SO网友:s_ha_dum

如果您的表格已作为page template, 那么我鼓励你get_permalink(8); // or 12 应该这样做。

如果没有,则http://example.com/?p=8http://example.com/?p=12 应该始终有效。http://example.com/?page_id=8http://example.com/?page_id=12 对页面使用适当的参数,但?p= 测试时效果很好。

SO网友:5wpthemes

尝试如下设置所有字段:

步骤1。

//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;
    }
}
 ?>
步骤2。更正错误

<?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 } ?>
第3步。您的表单

<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>
即使看起来不一样,也很容易做到。你所需要的就是尝试更多!

如果仍要重定向到其他页面,请使用以下选项:

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;
}
}

结束

相关推荐

如何使Contact Form7弹出到厚框中?

我正在尝试在一个thickbox模式窗口中弹出联系人表单7。我可以使用do_shortcode 或者短代码,但我不知道如何设置它。有人能把我推向正确的方向吗?从Thickbox page, 看起来我应该使用内联内容、iFramed内容或AJAX内容,但我不确定是哪一种。