We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44234
    • 219 Posts
    Hi,

    I am trying to make a custom hook that submits the email address and name from a form to campaign monitor.

    I have added the code below as a hook and if I manually replace 'Subscriber email' + 'Subscriber name' with an email address and name then it works great. What I need to understand is how to dynamically replace them with the data from the form.

    <?php
    require_once '../../csrest_subscribers.php';
    
    $wrap = new CS_REST_Subscribers('{list id}', '{api key}');
    $result = $wrap->add(array(
        'EmailAddress' => 'Subscriber email',
        'Name' => 'Subscriber name',
        'CustomFields' => array(
            array(
                'Key' => 'Field Key',
                'Value' => 'Field Value'
            )
        ),
        'Resubscribe' => true
    ));
    
    echo "Result of POST /api/v3/subscribers/{list id}.{format}\n<br />";
    if($result->was_successful()) {
        echo "Subscribed with code ".$result->http_status_code;
    } else {
        echo 'Failed with code '.$result->http_status_code."\n<br /><pre>";
        var_dump($result->response);
        echo '</pre>';
    }


    Is anyone able to help or point me in the right direction please?

    Many Thanks
      Find me on Twitter, GitHub or Google+
    • Hi romanum,

      Fields are available within hooks with the hook PHP variable. getValue, getValues() methods might become handy here smiley

      $fieldname = $hook->getValue('fieldname');
      


      You might want to read http://rtfm.modx.com/display/ADDON/FormIt.Hooks#FormIt.Hooks-AccessingtheFormItfieldsintheHook & http://rtfm.modx.com/display/ADDON/FormIt.Examples.Custom+Hook

      Hope that will help
        • 44234
        • 219 Posts
        I managed to get this working, code below if it can help anyone else:

        <?php
        require_once '../../csrest_subscribers.php';
        
        $Subscription = $hook->getValue('Subscription');
        
        if ( $Subscription == Yes ) {
        	echo
        $wrap = new CS_REST_Subscribers('{list id}', '{api key}');
        $result = $wrap->add(
        $allFormFields = $hook->getValues(array(
            'EmailAddress' => 'EmailAddress',
            'Name' => 'Name',
            'CustomFields' => array(
                array(
                    'Key' => 'Field Key',
                    'Value' => 'Field Value'
                )),
            'Resubscribe' => true
        	))
        );
        } else {
        	echo "";
        }


        This hook adds users to a campaign monitor list using a form on my website. They will only be added if they selected a 'subscription' check box which passes a value of 'Yes'.

        This uses the Campaign Monitor API and the PHP Wrapper available from their website. You will need to change the list id and api key to your own.

        If any one has any improvements to this code please let me know

        Thanks
          Find me on Twitter, GitHub or Google+