douhuai4155 2014-12-18 17:16
浏览 67
已采纳

在wordpress自定义联系表单插件中提交表单后重置表单字段

I've my own custom contact form wordpress plugin, in which I'm unable to reset the form fields after the user submits the form.

When the user fills up all the fields and submits, success message will be shown, and after that I need the form to reset all the fields. At present it shows the values entered by the user before submission.

Any help is greatly appreciated. Thanks in advance. Here is my code.

<?php
/*
Plugin Name: Custom Contact Form
Plugin URI: 
Description: <code>[contact email="your@email.address"]</code>
Version: 1.0
Author: 
Author URI: 
*/

// function to get the IP address of the user
function tw_get_the_ip() {
    if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
        return $_SERVER["HTTP_X_FORWARDED_FOR"];
}
elseif (isset($_SERVER["HTTP_CLIENT_IP"])) {
    return $_SERVER["HTTP_CLIENT_IP"];
}
else {
    return $_SERVER["REMOTE_ADDR"];
}
}

// the shortcode
function tw_contact_form_sc($atts) {
extract(shortcode_atts(array(
    "email" => get_bloginfo('admin_email'),
    "subject" => '',
    "label_name" => 'Your Name',
    "label_email" => 'Your E-mail Address',
    "label_subject" => 'Subject',
    "label_message" => 'Your Message',
    "label_submit" => 'Submit',
    "error_empty" => 'Please fill in all the required fields.',
    "error_noemail" => 'Please enter a valid e-mail address.',
    "success" => 'Thanks for your e-mail! We\'ll get back to you as soon as we can.'
), $atts));

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $error = false;
    $required_fields = array("your_name", "email", "message", "subject");

    foreach ($_POST as $field => $value) {
        if (get_magic_quotes_gpc()) {
            $value = stripslashes($value);
        }
        $form_data[$field] = strip_tags($value);
    }

    foreach ($required_fields as $required_field) {
        $value = trim($form_data[$required_field]);
        if(empty($value)) {
            $error = true;
            $result = $error_empty;
        }
    }

    if(!is_email($form_data['email'])) {
        $error = true;
        $result = $error_noemail;
    }

    if ($error == false) {
        $email_subject = "[" . get_bloginfo('name') . "] " . $form_data['subject'];
        $email_message = $form_data['message'] . "

IP: " . tw_get_the_ip();
        $headers  = "From: ".$form_data['your_name']." <".$form_data['email'].">
";
        $headers .= "Content-Type: text/plain; charset=UTF-8
";
        $headers .= "Content-Transfer-Encoding: 8bit
";
        wp_mail($email, $email_subject, $email_message, $headers);
        $result = $success;
        $sent = true;
    }
}

if($result != "") {
    $info = '<div class="info">'.$result.'</div>';
}
$email_form = '<form class="contact-form" method="post" action="'.get_permalink().'">
    <div>
        <label for="cf_name">'.$label_name.':</label>
        <input type="text" name="your_name" id="cf_name" size="50" maxlength="50" value="'.$form_data['your_name'].'" />
    </div>
    <div>
        <label for="cf_email">'.$label_email.':</label>
        <input type="text" name="email" id="cf_email" size="50" maxlength="50" value="'.$form_data['email'].'" />
    </div>
    <div>
        <label for="cf_subject">'.$label_subject.':</label>
        <input type="text" name="subject" id="cf_subject" size="50" maxlength="50" value="'.$subject.$form_data['subject'].'" />
    </div>
    <div>
        <label for="cf_message">'.$label_message.':</label>
        <textarea name="message" id="cf_message" cols="50" rows="15">'.$form_data['message'].'</textarea>
    </div>
    <div>
        <input type="submit" value="'.$label_submit.'" name="send" id="cf_send" />
    </div>
</form>';

if($sent == true) {
        return $info.$email_form;
} else {
        return $info.$email_form;
}
} add_shortcode('contact', 'tw_contact_form_sc');

?>
  • 写回答

2条回答 默认 最新

  • douxing1850 2014-12-18 17:35
    关注

    In this instance you can just manually assign your values on $sent == true:

    // Presumably if the email is sent, you want blank values instead
    // of populated ones.
    if($sent == true) {
            $form_data['your_name'] =   "";
            $form_data['email']     =   "";
            $form_data['subject']   =   "";
            $form_data['message']   =   "";
        }
    
    $email_form = '<form class="contact-form" method="post" action="'.get_permalink().'">
        <div>
            <label for="cf_name">'.$label_name.':</label>
            <input type="text" name="your_name" id="cf_name" size="50" maxlength="50" value="'.$form_data['your_name'].'" />
        </div>
        <div>
            <label for="cf_email">'.$label_email.':</label>
            <input type="text" name="email" id="cf_email" size="50" maxlength="50" value="'.$form_data['email'].'" />
        </div>
        <div>
            <label for="cf_subject">'.$label_subject.':</label>
            <input type="text" name="subject" id="cf_subject" size="50" maxlength="50" value="'.$subject.$form_data['subject'].'" />
        </div>
        <div>
            <label for="cf_message">'.$label_message.':</label>
            <textarea name="message" id="cf_message" cols="50" rows="15">'.$form_data['message'].'</textarea>
        </div>
        <div>
            <input type="submit" value="'.$label_submit.'" name="send" id="cf_send" />
        </div>
    </form>';
    

    There are lots of ways to do this, but without doing much in the way of modifying what you have, this is probably the quickest.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?