This forum is no longer open and is for reading/searching only.

Please use our new MachForm Community Forum instead.

MachForm Community Forums » MachForm 2

PHP help needed to integrate form with Campaign Monitor API


  1. blogjunkie
    Member

    I'm trying to add names, email addresses and a country drop down field to Campaign Monitor via their API. I followed instructions from the forums and here is what I have done so far.

    First, inside post-functions.php, under $process_result['status'] = true; on line 1215 I added include 'custom.php';. This is the contents of custom.php:

    define('MACHFORM_DIR', '/path/to/machform');
    
    // include CM subscriber api class
    require_once (MACHFORM_DIR . '/lib/createsend-php/csrest_subscribers.php');
    
    if ($form_id == 1) {
    	$wrap = new CS_REST_Subscribers('MYAPIKEY', 'MYLISTID');
    	$result = $wrap->add(array(
    	    'EmailAddress' => $table_data['element_2'],
    	    'Name' => $table_data['element_3'],
    	    'CustomFields' => array(
    	        array(
    	            'Key' => 'Country',
    	            'Value' => '$table_data[\'element_15\']', // NEED TO FIGURE OUT THE INDEX FOR THIS
    	            'Key' => 'DateofBirth',
    	            'Value' => '$table_data[\'element_4\']'
    	        )
    	    ),
    	    'Resubscribe' => true
    	));
    }
    

    A little lower in my post-functions.php under $process_result['status'] = true;, I added this code:

    $_SESSION['form_data'] = $table_data;
    

    Next on the PHP page that I display the form, I have this:

    <?php
    	session_start();
    	require("/path/to/machform/machform.php");
    	$mf_param['form_id'] = 1;
    	$mf_param['base_path'] = 'http://www.example.com/machform/';
    	display_machform($mf_param);
    ?>
    
    <?php print_r($_SESSION['form_data']); ?>
    

    The problem that I'm facing:

    1. When I submit the form, I get a message saying "Your session has been expired. Please start again." and the data is not saved to Machform

    2. I want to see the form_data array so I can figure out the values for Country and DateofBirth to send to Campaign Monitor.

    I think it must be something simple but I just can't find it. Please help, thanks

    Posted 14 years ago #
  2. yuniar

    1) On the page where you embed the form, line 1, insert the following code:

    session_start();

    that should start the session on that page and prevent the session expired error.

    2) The "$table_data" is the array. You can dump this array to see all the indexes.
    You can use this simple code:

    print_r($table_data);

    MachForm Founder

    Posted 14 years ago #
  3. blogjunkie
    Member

    Hi Yuniar, now my form embed code is like this:

    <?php
    	session_start();
    	require("/path/to/machform/machform.php");
    	$mf_param['form_id'] = 1;
    	$mf_param['base_path'] = 'http://example.com/machform/';
    	display_machform($mf_param);
    ?>
    <?php print_r($table_data); ?>
    

    This is what I'm getting now:

    Warning: session_start() [function.session-start]: Cannot send
    session cookie - headers already sent by (output started at
    /path/to/form.php:10) in /path/to/form.php on line 11
    

    Btw I enabled the review page feature.

    Posted 14 years ago #
  4. yuniar

    Sorry if I wasn't clear.
    The code should be placed into the first line of the page where you embed the form, not the first line of the form code.

    So, let say you put the form into myform.php file.
    Put the session_start(); code at the beginning of myform.php file.

    Regarding the form data array, I just noticed that you have copied the data into session variable, so you can ignore my last reply. You can use your original code:

    <?php print_r($_SESSION['form_data']); ?>

    MachForm Founder

    Posted 14 years ago #

RSS feed for this topic

Reply