user8001297
user8001297

Reputation: 173

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.

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 = '[email protected]';

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

Upvotes: 0

Views: 2161

Answers (2)

Gnanasekaran Loganathan
Gnanasekaran Loganathan

Reputation: 1108

Try below scenarios:

First: Use $_POST empty where you finish your process.

$_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:

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

Upvotes: 0

Arun
Arun

Reputation: 1719

You can use .reset() on your form.

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

Upvotes: 1

Related Questions