North Yorkshire, UK Kent, UK Chamonix, France Phone: 01905 570 270 Email: mint@createdbymint.co.uk

Mailchimp Custom Subscribe Example

Setting up the PHP code to use Mailchimp’s API

By Using the below PHP script and calling it using a URL variable to carry the email address value across, you have a basic mail list member subscription function for mailchimp using their API v3.0
You could for example call this script using some jquery to process a nice neat ajax request to the user can subscribe without experiencing page reloads.

function _sendRequest($method, $parameters) {

        // Construct array of parameter key and value pairs
        $pairs = json_encode($parameters);
        
        // Setup CURL and execute the HTTP request to mailchimp's API
		$handle = curl_init();
		
		curl_setopt($handle, CURLOPT_URL, 'https://us2.api.mailchimp.com/3.0/' . $method);
		curl_setopt($handle, CURLOPT_HTTPHEADER, array(                                                                          
					'Content-Type: application/json',
					'Authorization: [ADD YOUR USERNAME HERE] [ADD YOUR API KEY HERE]',
					'Content-Length: ' . strlen($pairs))                                                                       
					);		
		curl_setopt( $handle, CURLOPT_CUSTOMREQUEST, 'POST' );
		//curl_setopt( $handle, CURLOPT_CUSTOMREQUEST, $type );
		curl_setopt( $handle, CURLOPT_TIMEOUT, 5 );
		curl_setopt( $handle, CURLOPT_RETURNTRANSFER, true );
		curl_setopt($handle, CURLOPT_POSTFIELDS, $pairs);
		
		

		$result = curl_exec($handle);
		curl_close($handle);

            //returns the result as a JSON encoded string
	    return $result;
}

//setup the member parameters to pass into the function using the email address passed to the script via the URL variable $_GET['email']

$members = array('email_address'=>$_GET['email'], 'status'=>'subscribed');

//execute the function using the API method to subscribe to a particular list, and the parameters from the above line ($members) and echo the result. REMEMBER to add your list ID in the below line.

echo _sendRequest('lists/[ADD YOUR LIST ID HERE]/members', $members) ;

Calling the function

Now that the PHP is done, save that as a file on its own, or place the function where ever you like and create a file that calls it using the lines to create the parameters as $members, and echo back the funtion call _sendRequest(‘lists/[ADD YOUR LIST ID HERE]/members’, $members).

Putting it to work

Once thats done, you can create your html input field and button on the web page your want and use some jquery like the example below to put it all together (obviously amending it to perform actions as you want it to).

// when element with class 'subscribe-mailchimp' is clicked, execute function
$('.subscribe-mailchimp').click(function(){
		var $this = $(this);
                //get the value of the input field with ID 'email' inside the same parent element which contains the clicked 'subscribe-mailchimp' element
		data = $this.siblings('input#email').val();
		if (data == '') 
		{
			//no email entered - do what you want
		}
		else
		{
			$.getJSON('[URI FOR THE ABOVE SCRIPT - above.php]', {email: data}, function(data)
			{
				//console.log(data);
				if (data) 
				{
					if (data.type) 
					{
						//if error - do something 
						//console.log('data'); 
					}
					else if (data.id) 
					{
						//if done - do something 
						//console.log('data'); 
						//console.log('done'); 
					}
					else 
					{
						//if other error - do something 
						//console.log('data'); 
						//console.log('else'); 
					}
				} 
				else 
				{
					//if other error - do something 
					//console.log('data'); 
					//console.log('else'); 
				}
			})
			.fail(function()
			{
				//if failed to execute - do something
				//console.log('fail'); 
			});
		}
	})
php Leave a comment

Detect Even Numbers using PHP

Using PHP Math

We can use standard PHP math to detect even numbersĀ in one simple line of code by using the modulus operator (%). Which gives the remainder left over after a division, for example 5 divided by 2 would equal 2.5. Therefore giving a quotient of 2 and a remainder of 1. Whereas 6 divided by 3 equals 2, with a quotient of 3 and a remainder of 0. So the conditional statement using the modulus of any number by 2 will yield a one and zero answer for odds and evens. In conclusion “any number % 2” will evaluate to zero if the given number is even, and one if the given number is odd. Which is useful for outputting different markup during a loop.

//the condition
if ($number % 2 == 0)
{
   //do something if even numbers
} 
else
{
   //do something if odd numbers
}

CSS Alternative for formatting odd/even HTML markup

Although, if it’s just a case of trying to give some different formatting to an HTML table for example. It would be far simpler to just use the nth-child(odd)/nth-child(even) CSS pseudo selectors to target the odd and even items.

li:nth-child(even) { background-color:#eee; }

and

li:nth-child(odd) { background-color:#fff; }
php Leave a comment

Question? Give us a call.

Intelligent solutions for your online property be it simple or complex, just starting out or established. Get in touch with us, we'd love to help. Phone: 01905 570 270

Join our mailing list

We'll send you only occasional emails to share our latest ideas and offers with you.
So why not join up?

Interested in what we do?

Interested in what we do?

Get in touch

Contact Us