简体   繁体   English

在Wordpress中提交后清除表单字段

[英]Clearing the form fields after submit in wordpress

How to clear the for, fields once the data is submitted.After clicking on submit the data in the text box is not getting cleared. 提交数据后如何清除for字段。单击提交后,文本框中的数据未清除。

HTML: HTML:

function html_form_code() {
echo '<div class="thb_subscribe">';
echo '<h3 class="subscriberlogin">SUBSCRIBE</h3>';
echo '<p class="subscribertext">Subscribe now to get notified about latest updates!!</p>';
echo '<form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post">';

echo '<div class="emails" style="margin-left: -308px;">';
echo '<input type="email" id="cf-email" name="cf-email" placeholder="Your E-Mail" value="' . ( isset( $_POST["cf-email"] ) ? esc_attr( $_POST["cf-email"] ) : '' ) . '" size="42" required />';
echo '</div>';

echo '<div class="subjectline" style="display:none;">';
echo 'Subject (required) <br/>';
echo '<input type="text" name="cf-subject" pattern="[a-zA-Z ]+" value=" Subscription List"  />';
echo '</div>';

echo '<div class="signup"><input type="submit" name="cf-submitted" value="Sign Up"></div>';
echo '</form>';
echo '</div>';
 }

Script: 脚本:

jQuery(document).ready(function($) {
$('#cf-email').val(''); //txtID is textbox ID
});​

Tried with this code but it is not working. 尝试过使用此代码,但无法正常工作。

Sending Email: 发送电子邮件:

function deliver_mail() {

// if the submit button is clicked, send the email
if ( isset( $_POST['cf-submitted'] ) ) {

    // sanitize form values
    $email   = sanitize_email( $_POST["cf-email"] );
    $subject = sanitize_text_field( $_POST["cf-subject"] );
    $message = "This person $email  has been subscribed to your channel. ";

    // get the blog administrator's email address       
    $to = 'XXXX@gmail.com';

    $headers = "From:  <$email>" . "\r\n";

    // If email has been process for sending, display a success message
    if ( wp_mail( $to, $subject, $message, $headers ) ) {
        echo '<div>';
        echo '<div id="deletesuccess">Thanks for Subscribing .</div>';
        echo '</div>';
    } else {
        echo 'An unexpected error occurred';
    }
}
}

您可以在表单上使用.reset()。

$(".myform")[0].reset();

Try below scenarios: 请尝试以下情形:

First: Use $_POST empty where you finish your process. 第一:在完成过程的位置使用$ _POST空。

$_POST = array();

Second: Use below script: 第二:使用以下脚本:

 $("form").submit() {
    $(this).closest('form').find("input[type=text], textarea").val("");
 });

Third: After finish your process just referesh the page use PHP: 第三:完成过程后,只需使用PHP刷新页面即可:

header("Location: http://example.com/");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM