Skip to content

Commit fb6cacc

Browse files
committed
Make form reset for the multiple form on same page.
1 parent 8aec1e8 commit fb6cacc

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

assets/js/mailchimp.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,44 @@
22
(function ($) {
33
function mc_beforeForm() {
44
// Disable the submit button
5-
$('#mc_signup_submit').attr('disabled', 'disabled');
5+
$('.mc_signup_submit_button').attr('disabled', 'disabled');
66
}
77

8-
function mc_success(data) {
8+
function mc_success(data, status, xhr, submittedForm) {
9+
let form = $(submittedForm);
10+
if (!submittedForm || !form) {
11+
form = $('.mc_signup_form');
12+
}
13+
914
// Re-enable the submit button
10-
$('#mc_signup_submit').removeAttr('disabled');
15+
$('.mc_signup_submit_button').removeAttr('disabled');
1116

1217
// Put the response in the message div
13-
$('#mc_message').html(data);
18+
form.find('.mc_message_wrapper').html(data);
1419

1520
// See if we're successful, if so, wipe the fields
1621
const reg = /class=(["'])mc_success_msg\1/i;
1722

1823
if (reg.test(data)) {
19-
$('#mc_signup_form').each(function () {
24+
$(form).each(function () {
2025
this.reset();
2126
});
22-
$('#mc_submit_type').val('js');
27+
28+
$('.mc_submit_type').val('js');
2329
}
30+
2431
window.scrollTo({
25-
top: document.getElementById('mc_signup').offsetTop - 28,
32+
top: parseInt(form.offset().top, 10) - 28,
2633
behavior: 'smooth',
2734
});
2835
}
2936

3037
$(function ($) {
3138
// Change our submit type from HTML (default) to JS
32-
$('#mc_submit_type').val('js');
39+
$('.mc_submit_type').val('js');
3340

3441
// Attach our form submitter action
35-
$('#mc_signup_form').ajaxForm({
42+
$('.mc_signup_form').ajaxForm({
3643
url: window.mailchimpSF.ajax_url,
3744
type: 'POST',
3845
dataType: 'text',

mailchimp_widget.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ function mailchimp_sf_signup_form( $args = array() ) {
166166
?>
167167

168168
<div id="mc_signup">
169-
<form method="post" action="#mc_signup" id="mc_signup_form">
170-
<input type="hidden" id="mc_submit_type" name="mc_submit_type" value="html" />
169+
<form method="post" action="#mc_signup" id="mc_signup_form" class="mc_signup_form">
170+
<input type="hidden" id="mc_submit_type" class="mc_submit_type" name="mc_submit_type" value="html" />
171171
<input type="hidden" name="mcsf_action" value="mc_submit_signup_form" />
172172
<?php wp_nonce_field( 'mc_submit_signup_form', '_mc_submit_signup_form_nonce', false ); ?>
173173

@@ -183,7 +183,7 @@ function mailchimp_sf_signup_form( $args = array() ) {
183183

184184
<div class="mc_form_inside">
185185

186-
<div class="updated" id="mc_message">
186+
<div class="update mc_message_wrapper" id="mc_message">
187187
<?php echo wp_kses_post( mailchimp_sf_global_msg() ); ?>
188188
</div><!-- /mc_message -->
189189

@@ -270,7 +270,7 @@ function mailchimp_sf_signup_form( $args = array() ) {
270270
?>
271271

272272
<div class="mc_signup_submit">
273-
<input type="submit" name="mc_signup_submit" id="mc_signup_submit" value="<?php echo esc_attr( $submit_text ); ?>" class="button" />
273+
<input type="submit" name="mc_signup_submit" class="mc_signup_submit_button" id="mc_signup_submit" value="<?php echo esc_attr( $submit_text ); ?>" class="button" />
274274
</div><!-- /mc_signup_submit -->
275275

276276
<?php

0 commit comments

Comments
 (0)