Bootstrap模式中的WordPress自定义联系表单未显示验证和提交响应

时间:2018-07-13 14:06:50

标签: php wordpress modal-dialog bootstrap-4 contact-form

对于我的WordPress,我正在使用Bootstrap并使用下面的HTML构建了一个自定义联系人表单(本教程之后:https://premium.wpmudev.org/blog/how-to-build-your-own-wordpress-contact-form-and-why/)。

<form id="contact-form" action="<?php echo get_site_url(); ?>" method="post">

<div class="modal-body">
    <?php echo $response; ?>
    <div class="form-group">
        <input class="form-control my-2" type="text" name="message_name" size="50" placeholder="Your full name" value="<?php echo esc_attr($_POST['message_name']); ?>">
        <input class="form-control my-2" type="email" name="message_email" size="50" placeholder="Email address" value="<?php echo esc_attr($_POST['message_email']); ?>">
        <input class="form-control my-2" type="tel" name="message_tel" size="50" placeholder="Country code, Phone number" value="<?php echo esc_attr($_POST['message_tel']); ?>">
        <textarea class="form-control my-2" name="message_text" rows="2" placeholder="Your message" value="<?php echo esc_attr($_POST['message_text']); ?>"></textarea>
        <input  class="form-control my-2" type="text" name="message_human" placeholder="Human check: Enter 2">
        <input type="hidden" name="message_url" value="<?php the_permalink(); ?>">
        <input type="hidden" name="message_page" value="<?php the_title(); ?>">
    </div>
</div>
<div class="modal-footer">
    <input type="hidden" name="submitted" value="1">
    <button type="submit" value="Submit" class="btn btn-search form-control">Send Enquiry</button>
</div>

下面的功能用于验证表单并在提交表单时显示响应:

//response generation function
$response = "";

//function to generate response
function contact_g_form_response($type, $message) {

global $response;

if ($type == "success") {
    $response = "<div class='message-success text-center'>{$message}</div>";
} else {
    $response = "<div class='message-error text-center'>{$message}</div>";
}
}

//response messages
$not_human = "Enter current year in numbers.";
$missing_content = "Missing something.";
$email_invalid = "Check your Email address.";
$message_unsent = "Message was not sent. Try Again.";
$message_sent = "Thanks! We got your enquiry.";

//user posted variables
$name = $_POST['message_name'];
$email = $_POST['message_email'];
$fromEmail = $name . '<' . $email . '>';
$tel = $_POST['message_tel'];
$text = $_POST['message_text'];
$url = $_POST['message_url'];
$page = $_POST['message_page'];
$human = $_POST['message_human'];


//php mailer variables
$to = get_option('admin_email');
$subject = '[General Enquiry] ' . $name . ' | Phone Number:' . $tel;
$headers = 'From: ' . $fromEmail . "\r\n" .
    'Reply-To: ' . $email . "\r\n";

$message = '<html><body><h1>New general enquiry from ' . $name . '!</h1>'
    . '<p>Email: ' . $email . '</p>'
    . '<p>Phone Number: ' . $tel . '</P>'
    . '<p>Inquiry: ' . $text . '</P>'
    . '<p>From page: <b>' . $page . '</b></P>'
    . '<p>Page URL: ' . $url . '</p>'
    . '</body></html>';

if (!$human == 0) {
if ($human != 2) {
    contact_g_form_response("error", $not_human); //not human!
} else {

    //validate email
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        contact_g_form_response("error", $email_invalid);
    } else { //email is valid
        //validate presence of name, phone number
        if (empty($name) || empty($tel)) {
            contact_g_form_response("error", $missing_content);
        } else { //ready to go!
            $sent = wp_mail($to, $subject, $message, implode("\r\n", $headers)); //mail to admin - striptags removing formatting
            // $sent2 = wp_mail($email, $subject, $body, $headers); //mail to visitor
            // if ($sent || $sent2) {
            if ($sent) {
                contact_g_form_response("success", $message_sent); //message sent!
            } else {
                contact_g_form_response("error", $message_unsent); //message wasn't sent
            }
        }
    }
}
} else if ($_POST['submitted']) {
contact_g_form_response("error", $missing_content);
}

尽管成功提交了表单,但未按照contact_g_form_response函数显示任何响应/验证消息,并且在表单提交后,它已被重定向到随机的WordPress帖子。

1 个答案:

答案 0 :(得分:1)

您只需要替换以下内容:

<form id="contact-form" action="<?php echo get_site_url();?>" method="post">

与此:

<form id="contact-form" method="post">

因为您在同一页面上显示错误,但是单击提交按钮后,操作会将您带到特定的url,这就是未显示验证的原因。

还有:

if ($type == "success") {
$response = "<div class='message-success text-center'>{$message}</div>";
} else {
$response = "<div class='message-error text-center'>{$message}</div>";
}

与此同时:

if ($type == "success") {
$response =  $message;
echo $response;
} else {
$response =  $message;
echo $response;
}