Skip to content

Need examples for multiple value multi-option custom fields with AJAX #99

@MichaelPrecel

Description

@MichaelPrecel

Hey there,

I'm trying to capture data from a form to multi-option custom fields with multiple inputs.

I have it all set up, but need examples to demonstrate pulling in multiple values and passing that through the 'CustomFields' array. The examples suggest repeating the array with the same 'Key' and different 'Values', eg

'CustomFields' => array(
                array(
                  'Key' => 'Interested',
                  'Value' => '1 Bedroom'
                ),
                array(
                  'Key' => 'Interested',
                  'Value' => '2 Bedroom'
                )
            ),

This works fine. However this doesn't show how to dynamically add these arrays into the 'CustomFields' array. For example by pulling the data in with AJAX, processing the JSON array into PHP and then adding to the 'CustomFields' array. Are you able to please help with this?

An example:

HTML

<fieldset class="reasons__panel interested__panel" id="interested__panel">
                <label class="checkbox__outer">1 Bedroom Home
                    <input class="interested__input"  type="checkbox" name="interested[]" value="1 Bedroom Home"><br>
                    <span class="checkmark"></span>
                </label>
                <label class="checkbox__outer">2 Bedroom Home
                    <input class="interested__input"  type="checkbox" name="interested[]" value="2 Bedroom Home"><br>
                    <span class="checkmark"></span>
                </label>
                <label class="checkbox__outer">3 Bedroom Home
                    <input class="interested__input"  type="checkbox" name="interested[]" value="3 Bedroom Home"><br>
                    <span class="checkmark"></span>
                </label>
                <label class="checkbox__outer">Cafes & Restaurants
                    <input class="interested__input"  type="checkbox" name="interested[]" value="Cafes & Restaurants"><br>
                    <span class="checkmark"></span>
                </label>
                <label class="checkbox__outer">Retail
                    <input class="interested__input"  type="checkbox" name="interested[]" value="Retail"><br>
                    <span class="checkmark"></span>
                </label>
                <label class="checkbox__outer">Office & Co — Working
                    <input class="interested__input"  type="checkbox" name="interested[]" value="Office & Co — Working"><br>
                    <span class="checkmark"></span>
                </label>
            </fieldset>

JQuery

// AJAX Submit form to CAMPAIGN MONITOR
        // Get data from form and store it
        var cmSignupName = $('#input--name').val();
        var cmSignupEmail = $('#input--email').val();
        var cmSignupPhone = $('#input--phone').val();
        var cmSignupPostcode = $('#input--postcode').val();
        var cmSignupInterested = [];
        $('.interested__input:checked').each(function(){
          var interested = $(this).val();
          cmSignupInterested.push(interested);
        });
        var cmSignupComments = $('#input--comments').val();

        // Create JSON variable of retrieved data
        var cmSignupData = {
          'name': cmSignupName,
          'email': cmSignupEmail,
          'phone': cmSignupPhone,
          'postcode': cmSignupPostcode,
          'Comments': cmSignupComments,
          'Interested': cmSignupInterested
        };


        // Send data to PHP script via AJAX
        $.ajax({
          url: $(this).attr('action') + '/cm',
          type: 'POST',
          dataType: 'json',
          data: cmSignupData, 
        });

I've attempted plenty of ways of achieving this in PHP, but I cannot get this to work. I am guessing a new array is needing to be stored in a variable and inputted as the 'CustomFields' value? The code below is terrible, but just for a demonstration of what I need to achieve.

PHP

$interested = json_decode( $_POST['Interested'] );
        $interested__array = array();

        foreach ( $interested as $item ) {
          array_push($interested__array, $item);
        }


        $result = $wrap->add(array(
            'EmailAddress' => $_POST['email'],
            'Name' => $_POST['name'],
            'CustomFields' => array(
                array(
                    'Key' => 'phone',
                    'Value' => $_POST['phone']
                ),
                array(
                    'Key' => 'postcode',
                    'Value' => $_POST['postcode']
                ),
                array(
                  'Key' => 'Comments',
                  'Value' => $_POST['Comments']
                ),
                $interested__array
            ),
            'ConsentToTrack' => 'yes',
            'Resubscribe' => true
        ));

Any and all help is much appreciated!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions