• Resolved Margot Keller

    (@margotk)


    Hello there,

    I use JavaScript form events, but I would like to do an action if the form succeed OR if the user is already subscribed.
    I didn’t get where I can have the status of the response in order to differentiate it.

    I’ve tried different events and this is what I get:

    submitted
    if already subscribed: data = null
    if new: data = null
    success
    Nothing return if already subscribed

    subscribed
    Nothing return if already subscribed

    This is the code:

    mc4wp.forms.on('submitted', function(form, data) {
       console.log(data);
    });

    Precision: it’s not a form used to update. (It’s a simple newsletter subscription form)

    I don’t need specially the content of data, I just need a criterion to differentiate “successful/already registered” from “potential errors”.

    Thank you for your help !

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Lap

    (@lapzor)

    Please look at this snippet:

    <script type="text/javascript">
    mc4wp.forms.on('subscribed', function(form, data, updated) {
      if(!updated) {
        console.log('This is new subscriber');
      } else {
        console.log('This is an updated subscriber');
      }
    })
    </script>

    https://www.mc4wp.com/kb/javascript-form-events/

    Hope that helps. If you have any questions, please let me know!

    Thread Starter Margot Keller

    (@margotk)

    Hello,

    thanks for the reply,

    I tested this snippet, and if the user is already subscribed, It does not fit in the condition at all.
    If it’s a new subscriber, yes it log “This is new subscriber” well.

    It seems that the event “subscribed” is not happen if the user is already subscribed.

    Plugin Contributor Lap

    (@lapzor)

    So “update subscriber” is set to No in the form’s settings?
    One possible solution could be to turn update to Yes. It won’t overwrite fields that aren’t in this particular form.

    Otherwise, if updating subscribers is not an option for your case, you would have to look for the ‘success’ event, that will fire whenever the form is submitted without errors, and separately look for the ‘subscribed’ event. In the case of existing subscriber only the success event will trigger, otherwise BOTH will trigger.

    There is no way to check from within the success event if it resulted in subscribing or not.

    Kind regards,

    Thread Starter Margot Keller

    (@margotk)

    “update subscriber” is now set to Yes, and it works !

    This solution is fine with me, and the other one proposed seems fine too.

    Thanks a lot!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘JavaScript form events data null’ is closed to new replies.