Yoko
Yoko

Reputation: 49

Ajax javascript display message under button

I change ths page because I found the solution, I hope it can help some people.

This is the form to include in your web page update in function your code :

<div class="footerTemplateBox footerTemplateInformation">
  <div><br /><h2><?php echo OSCOM::getDef('module_footer_multi_template_mailchimp_text'); ?></h2></div>
  <div class="row">
  <?php echo HTML::form('mailchimp', OSCOM::link('index.php'), 'get', 'id="signupft"'); ?>
    <div class="col-md-12"><?php echo HTML::inputField('email', '', 'required  id="emailft" placeholder="' . OSCOM::getDef('entry_email_address') . '"', 'email') . HTML::hiddenField('anonymous', 'anonymous'); ?></div>
    <div class="col-md-12"><?php echo HTML::button(OSCOM::getDef('module_footer_multi_template_mailchimp_submit'), 'fa fa-send', null, 'info', ['params' => 'id="SendButton"']); ?></div>
  </form>
  </div>
  <div class="messageft" id="message"></div>
</div>

The script with code Becarefull, you must use jquery v3.0 : Inserted debug element if you have a problem

    $(document).ready(function() {
      $(\'#signupft\').submit(function() {
        $("#messageft").html("<div class=\"alert alert-info\" style=\"padding:5px; margin-top:5px;\" role=\"alert\">' . MODULES_HEADER_TAGS_MAILCHIMP_SUBMIT_MESSAGE . '</div>");
        $.ajax({
          url: \'ext/api/mailchimp_v3/subscribe.php\', // proper url to your "store-address.php" file
          type: \'POST\', // <- IMPORTANT

          data: $(\'#signupft\').serialize() + \'&ajax=true\',
          success: function(msg) {
    /*alert(msg);*/
          msg = msg.replace(/string\([0-9]*\) "/, \'\');
    /*alert(msg);*/
          msg = msg.replace(\'}"\', \'}\');
    /*alert(msg);*/
          var message = JSON.parse(msg);
            resultmessage = \'\';
            if (message.status === \'' . MODULES_HEADER_TAGS_MAILCHIMP_STATUS_CHOICE . '\') { // success
              resultmessage = \'<div class="alert alert-success alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>' . MODULES_HEADER_TAGS_MAILCHIMP_SUCCESS_MESSAGE . '</div>\'; // display the message
            } else if (message.status === 400) { // error e-mail exists
              resultmessage = \'<div class="alert alert-warning alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>' . MODULES_HEADER_TAGS_MAILCHIMP_ERROR_EXISTS_MESSAGE . '</div>\'; // display the message
            } else { // undefined error
              resultmessage = \'<div class="alert alert-danger alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>' . MODULES_HEADER_TAGS_MAILCHIMP_ERROR_MESSAGE . '</div>\'; // display the message
            }
            $(\'#messageft\').html(resultmessage); // display the message
            $(\'#fnameft\').val(""); // reset input field
            $(\'#lnameft\').val(""); // reset input field
            $(\'#emailft\').val(""); // reset input field
          }
        });
        return false;
      });
    });

files called by the ajax script

  include('ext/api/mailchimp_v3/MailChimp.php');

  $key = MODULES_HEADER_TAGS_MAILCHIMP_API;

  if ( isset($_POST['anonymous'])) {
    $list_id = MODULES_HEADER_TAGS_MAILCHIMP_LIST_ANONYMOUS;
    $merge_vars = [
                    'FNAME' => '',
                    'LNAME' => ''
                  ];

  } else {
    $list_id = MODULES_HEADER_TAGS_MAILCHIMP_LIST_CUSTOMERS;

    $merge_vars = [
                    'FNAME' => $_POST['firstname'],
                    'LNAME' => $_POST['lastname']
                  ];
  }

  $array = [
              'email_address' => $_POST['email'],
              'merge_fields'  => $merge_vars,
              'status'        => MODULES_HEADER_TAGS_MAILCHIMP_STATUS_CHOICE
            ];


  if (MODULES_HEADER_TAGS_MAILCHIMP_STATUS_CHOICE == 'pending') {
    $status = 'pending';
  } else {
    $status = 'subscribed';
  }

  $mc = new \MailChimp($key);

// add the email to your list
  $result = $mc->post('/lists/' . $list_id . '/members', $array);

//send
  $result = json_encode($result);

// If being called via ajax, run the function, else fail - console
  if ( MODULES_HEADER_TAGS_MAILCHIMP_DEBUG == 'True') {
    if ($_POST['ajax']) {
      var_dump($result); // send the response back
    } else {
      var_dump('Method not allowed - please ensure JavaScript is enabled in this browser');
    }
  } else {
    echo $result;
  }

Upvotes: 0

Views: 202

Answers (2)

madalinivascu
madalinivascu

Reputation: 32354

Select the message relative to your form if you have multiple forms

form.find(".message").html('<div class="alert alert-info" style="padding:5px; margin-top:5px;" role="alert">member exists already</div>');

don't forget to create the form variable before the ajax var form = $(this)

Upvotes: 0

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

Change this line:

console.log('member exists already')

to

$('#message').html('member exists already');

Upvotes: 1

Related Questions