[eluser]doubleplusgood[/eluser]
Hey there,
I was wondering if anyone has had experience producing a newsletter signup form that connects to a Mailchimp list?
I'm trying to find a sample that will set some flashdata success and failure messages also. Attempted this myself by placing the mailchimp mcapi class as a library and then calling this in my newsletter controller;
Code:
class Newsletter extends Controller {
function Newsletter()
{
parent::Controller();
}
function storeAddress(){
$data['email'] = $this->input->post('email');
$data['api'] = 'API';
$data['list_id'] = 'LISTID';
$this->load->library('Mcapi',$data);
// Validation
if(!$data['email']){
$this->session->set_flashdata('invalid_email', 'Email address is invalid');
}
if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $data['email'])) {
$this->session->set_flashdata('invalid_email', 'Email address is invalid');
}
if($this->mcapi->listSubscribe($data['list_id'], $data['email'], '') === true) {
// It worked!
$this->session->set_flashdata('success', 'Please check your email to confirm signup');
}else{
// An error ocurred, return error message
// return 'Error: ' . $api->errorMessage;
$this->session->set_flashdata('error', 'Something went wrong');
}
}
}
Simple signup form in my view;
Code:
<?php echo form_open('newsletter/storeAddress'); ?>
<?=form_input('email', set_value('email'));?>
<?=form_submit('submit', 'Add')?>
<?php echo form_close(); ?>
If anyone has any advice/pointers, I would be very grateful.
Thank you.