1

How can I use an HTML array with AMPscript and save each array item in it's own data extension field? For example, when using a group of checkboxes. The following snippet is slightly dumbed down—but works as expected for other HTML input types. Thanks!

%%[
    [name="FormPost";type="POST"]

    var @de, @prop, @updateOptions, @saveOptions, @de_statusCode, @de_statusMsg, @errorCode

    set @de = CreateObject( "DataExtensionObject" )
    SetObjectProperty( @de, "CustomerKey", "My-Data-Extension-Key" )

    set @prop = CreateObject( "APIProperty" )
    SetObjectProperty( @prop, "Name", "EmailAddress" )
    SetObjectProperty( @prop, "Value", RequestParameter( "emailaddress" ) )
    AddObjectArrayItem( @de, "Keys", @prop )

    /*
        How do I grab the myCheckbox array and save each value 
        in it's own field in my data extension? e.g. Afterwards
        I want field one to contain "Somthing", field two would 
        have "Another", and so on.
    */
    set @mycheckboxes = RequestParameter( 'myCheckbox' )

    /*
        The following doesn't save anything
        I would expect to have 4 blocks like this,
        one for each checkbox input.
    */
    set @prop = CreateObject( "APIProperty" )
    SetObjectProperty( @prop, "Name", "Checkbox1" )
    SetObjectProperty( @prop, "Value", @mycheckboxes )
    AddObjectArrayItem( @de, "Properties", @prop )

    set @updateOptions = CreateObject( "UpdateOptions" )
    set @saveOptions = CreateObject( "SaveOption" )
    SetObjectProperty( @saveOptions, "PropertyName", "DataExtensionObject" )
    SetObjectProperty( @saveOptions, "SaveAction", "UpdateAdd" )
    AddObjectArrayItem( @updateOptions, "SaveOptions", @saveOptions )

    set @de_statusCode = InvokeUpdate( @de, @de_statusMsg, @errorCode, @updateOptions )

    if @de_statusCode != "OK" then
        RaiseError( @de_statusMsg, 0, @de_statusCode, @errorCode )
    endif
]%%

<form action="%%= RequestParameter('PAGEURL') =%%" method="POST">
    <input type="checkbox" name="myCheckbox[]" value="Something">
    <input type="checkbox" name="myCheckbox[]" value="Another">
    <input type="checkbox" name="myCheckbox[]" value="Foobar">
    <input type="checkbox" name="myCheckbox[]" value="Blah">
    <input type="hidden" name="emailaddress" value="[email protected]">
    <input type="Submit" value="Submit">
</form>

1 Answer 1

0

I ended up handling this using only strings, as I wasn't able to find any real AMPscript functions to handle arrays in this manor. In my previous code, by removing [] from each checkbox name; during POST myCheckbox is just a string—which is then stored in the data extension. Updated code below:

%%[
    /* Variables for use in our HTML below */
    set @email = '[email protected]'
    set @my_checkboxes = lookup( 'My-Data-Extension-Name', 'My-Data-Extension-Field-Name', 'EmailAddress', @email )

    /*
        Once the form has POST'd and saved @my_checkboxes above
        will contain a string like "Something,Another,Foobar,Blah"
    */
]%%
%%[
    /* Handle POST form submission */
    [name="FormPost";type="POST"]

    var @de, @prop, @updateOptions, @saveOptions, @de_statusCode, @de_statusMsg, @errorCode

    set @de = CreateObject( "DataExtensionObject" )
    SetObjectProperty( @de, "CustomerKey", "My-Data-Extension-Key" )

    set @prop = CreateObject( "APIProperty" )
    SetObjectProperty( @prop, "Name", "EmailAddress" )
    SetObjectProperty( @prop, "Value", RequestParameter( "emailaddress" ) )
    AddObjectArrayItem( @de, "Keys", @prop )

    /* myCheckbox Field */
    set @my_checkboxes = RequestParameter( 'myCheckbox' )

    /*
        If I select all 4 checkboxes and submit
        @my_checkboxes will contain a string of all 4 input value's
        i.e. "Something,Another,Foobar,Blah"
    */

    if not isnull( @my_checkboxes ) then
        set @prop = CreateObject( "APIProperty" )
        SetObjectProperty( @prop, "Name", "My-Data-Extension-Field-Name" )
        SetObjectProperty( @prop, "Value", @my_checkboxes )
        AddObjectArrayItem( @de, "Properties", @prop )
    endif

    set @updateOptions = CreateObject( "UpdateOptions" )
    set @saveOptions = CreateObject( "SaveOption" )
    SetObjectProperty( @saveOptions, "PropertyName", "DataExtensionObject" )
    SetObjectProperty( @saveOptions, "SaveAction", "UpdateAdd" )
    AddObjectArrayItem( @updateOptions, "SaveOptions", @saveOptions )

    set @de_statusCode = InvokeUpdate( @de, @de_statusMsg, @errorCode, @updateOptions )

    if @de_statusCode != "OK" then
        RaiseError( @de_statusMsg, 0, @de_statusCode, @errorCode )
    endif
]%%

<form action="%%= RequestParameter('PAGEURL') =%%" method="POST">
    <fieldset>
        <legend>Some checkboxes</legend>
        <!--

            To check a previously checked checkbox we simply use indexof() to
            deteremine if the current checkbox value is within our @my_checkboxes string.
            e.g. "Is 'Something' in the string 'Something,Another,Foobar,Blah'"

        -->
        <input type="checkbox" name="myCheckbox" value="Something" %%[ if 0 != indexof( @my_checkboxes, 'Something' ) then ]%%checked="checked"%%[ endif ]%%>
        <input type="checkbox" name="myCheckbox" value="Another" %%[ if 0 != indexof( @my_checkboxes, 'Another' ) then ]%%checked="checked"%%[ endif ]%%>
        <input type="checkbox" name="myCheckbox" value="Foobar" %%[ if 0 != indexof( @my_checkboxes, 'Foobar' ) then ]%%checked="checked"%%[ endif ]%%>
        <input type="checkbox" name="myCheckbox" value="Blah" %%[ if 0 != indexof( @my_checkboxes, 'Blah' ) then ]%%checked="checked"%%[ endif ]%%>
    </fieldset>        
    <input type="hidden" name="emailaddress" value="[email protected]">
    <input type="Submit" value="Submit">
</form>
1
  • you could try JSON.stringify Commented Nov 11, 2020 at 18:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .