The Community is hosting an End of Summer sweepstakes! Participants must complete tasks to earn tickets that will enter them with a chance to win a free year of Constant Contact and other great prizes!*
*No Purchase Necessary. For Official Rules, visit here. Constant Contact’s End of Summer 2020 Sweepstakes ends on October, 20, 2020 at 11:50 PM EST.

....NEED Advice : Sample Contact Upload/Signup Forms & Required Fields

SOLVED
Go to solution
Highlighted
Consulting & Training

....NEED Advice : Sample Contact Upload/Signup Forms & Required Fields

I have implemented this:

 

https://github.com/constantcontact/Constant-Contact-PHP-Sample-Contact-Forms

 

The forms are working but I need some assistance with the code. In this sample contact forms they developed, they set "Required fields are: Email Address and at least one Contact List should be checked."

 

I need to set more required fields for an email to be added. Where can I do this or where can i add the code to so that I could have more required fields such as First name, Company Name, Custom Field 1 and Custome Field 2.

 

Any one have suggestions or can assist me with this?

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Honored Contributor

Re: ....NEED Advice : Sample Contact Upload/Signup Forms & Required Fields

Hello,

 

Based on your feedback, I took my concept code and performed more testing and found that this change was able to correct the issue.

 

I changed this:

//CHECKER
        if (!isset($postFields["custom_fields"]["2"]))
        {
            throw new Exception("You are missing a custom_field_2 value");
        }
		//CHECKER

 

To this:

		//CHECKER
		if ($postFields["custom_fields"][2] == "")
        {
            throw new Exception("You are missing a custom_field_2 value");
        }
		//CHECKER

 

Sincerely,

Elijah G.

Tier 2 Support

Elijah G.
API Support Engineer

View solution in original post

8 REPLIES 8
Highlighted
Honored Contributor

Re: ....NEED Advice : Sample Contact Upload/Signup Forms & Required Fields

Hello,

 

With our PHP sample signup forms that are provided, there is not a built-in functionality for verifying mutliple required fields. By default, the opnly fields that will be required is a list and an email address as the API request will fail without at least thow two items.

 

One option would be to manually add verification to the PHP code that submits the contact data. I have included below the code from the simple_form.php example that collects form information.

 

if (!empty($_POST)) {
		$postFields = array();
		$postFields["email_address"] = $_POST["email_address"];
		$postFields["first_name"] = $_POST["first_name"];
		$postFields["last_name"] = $_POST["last_name"];
		$postFields["middle_name"] = $_POST["middle_name"];
		$postFields["home_number"] = $_POST["home_num"];
		$postFields["address_line_1"] = $_POST["adr_1"];
		$postFields["address_line_2"] = $_POST["adr_2"];
		$postFields["address_line_3"] = $_POST["adr_3"];
		$postFields["city_name"] = $_POST["city"];
		$postFields["state_code"] = $_POST["state"];
		// The Code is looking for a State Code For Example TX instead of Texas
		$postFields["state_name"] = $_POST["state_name"];
		$postFields["country_code"] = $_POST["country"];
		$postFields["zip_code"] = $_POST["postal_code"];
		$postFields["sub_zip_code"] = $_POST["sub_postal"];
        if(isset($_POST["mail_type"]))
		{
		$postFields["mail_type"] = $_POST["mail_type"];
		}
		else
		{
			$postFields["mail_type"] = "HTML";
		}
		
		if(isset($_POST["lists"]))
		{
		$postFields["lists"] = $_POST["lists"];
		}
		else
		{
			$highlight_lists = true;
			throw new Exception("You are missing your list");
		}
		$contactXML = $ccContactOBJ->createContactXML(null,$postFields);

		if (!$ccContactOBJ->addSubscriber($contactXML)) {
			$error = true;
		} else {
			$error = false;
			$_POST = array();
		}
		
		


	}

 

Within this code, you could add additional verification similar to the list verification:

		if(isset($_POST["lists"]))
		{
		$postFields["lists"] = $_POST["lists"];
		}
		else
		{
			$highlight_lists = true;
			throw new Exception("You are missing your list");
		}

 

 

While this is somewhat complex, any method to add form verification before or after submitting the form would need to be added. Because the nature of these samples is to show how to submit information to the Constant Contact API, this is not a feature that was added to them.

 

If you have any questions for me, please reply to this post and I will be happy to assist you!

 

Sincerely,

Elijah G.

Tier 2 Support

Elijah G.
API Support Engineer
Highlighted
Consulting & Training

Re: ....NEED Advice : Sample Contact Upload/Signup Forms & Required Fields

Hi Elijah_G,

 

So if i add this code after the main checker for "lists", would it work?:

 

		if(isset($_POST["custome_field_2"]))
		{
		$postFields["custome_field_2"] = $_POST["custome_field_2"];
		}
		else
		{
			throw new Exception("You are missing a custome_field_2 value");
		}

 

Highlighted
Honored Contributor

Re: ....NEED Advice : Sample Contact Upload/Signup Forms & Required Fields

Hello,

 

I order to validate on a specific custom field it is a bit more complex. Because there are many custom fields, the sample form is set up to collect all of the information from the custom fields in a loop. The best way to accomplish this would be to add a check affter this code:

		$postFields["custom_fields"] = array();
		foreach($_POST as $key=>$val) {
			
			if (strncmp($key, 'custom_field_', strlen('custom_field_')) === 0) {
				$postFields["custom_fields"][substr($key, strlen('custom_field_'), strlen($key)-1)] = $val;
			}

		}

 

That check could look something like this (please note this is an example and you may need to re-create or modify it to work with your code):

        if (!isset($postFields["custom_fields"]["2"]))
        {
            throw new Exception("You are missing a custom_field_2 value");
        }

 

Sincerely,

Elijah G.

Tier 2 Support

Elijah G.
API Support Engineer
Highlighted
Consulting & Training

Re: ....NEED Advice : Sample Contact Upload/Signup Forms & Required Fields

Hey There,

 

Just wanted to firstly thank you for assiting me with this. I have added the following but it doesnt seem to be giving me the error if the custom field 2 is empty.

 

		$postFields["custom_fields"] = array();
		foreach($_POST as $key=>$val) {
			
			if (strncmp($key, 'custom_field_', strlen('custom_field_')) === 0) {
				$postFields["custom_fields"][substr($key, strlen('custom_field_'), strlen($key)-1)] = $val;
			}

		}
		//CHECKER
        if (!isset($postFields["custom_fields"]["2"]))
        {
            throw new Exception("You are missing a custom_field_2 value");
        }
		//CHECKER

 

Highlighted
Honored Contributor

Re: ....NEED Advice : Sample Contact Upload/Signup Forms & Required Fields

Hello,

 

Based on your feedback, I took my concept code and performed more testing and found that this change was able to correct the issue.

 

I changed this:

//CHECKER
        if (!isset($postFields["custom_fields"]["2"]))
        {
            throw new Exception("You are missing a custom_field_2 value");
        }
		//CHECKER

 

To this:

		//CHECKER
		if ($postFields["custom_fields"][2] == "")
        {
            throw new Exception("You are missing a custom_field_2 value");
        }
		//CHECKER

 

Sincerely,

Elijah G.

Tier 2 Support

Elijah G.
API Support Engineer

View solution in original post

Highlighted
Consulting & Training

Re: ....NEED Advice : Sample Contact Upload/Signup Forms & Required Fields

Ok that did the trick! amazing. Now my next battle:

 

Take this scenario:

 

I select the mailing group and place an email address, first name of the client, the business name etc... and don't place custom_field_2, I will get the error as desired. 

 

The selected mailing group become unchecked, while the rest of the information will be present until i place the custom field and mailing group again.

 

Is there any way not to force the mailing group check marks to reset if an exception eco's or appears?

Highlighted
Honored Contributor

Re: ....NEED Advice : Sample Contact Upload/Signup Forms & Required Fields

Hello,

 


Unfortunately there's not a functionality built into the sample form to remember those specific fields. While it may be possible to add that functionality, this form is reasonably limited in its scope as it is intended as a basic sample of how a signup form works with the PHP wrapper. If it is not able to fit the specifications you need, you might want to look into using the CCSFG or possibly creating your own form. If youare interested in the second option, it may be worthwhile to look into the newer version of the PHP wrapper.

 

Sincerely,

Elijah G.

Tier 2 Support

Elijah G.
API Support Engineer
Highlighted
Consulting & Training

Re: ....NEED Advice : Sample Contact Upload/Signup Forms & Required Fields

Fair enough, fair enough.

 

Would there be a possibily where i can change the way this is displayed:

 

exception 'Exception' with message 'You are missing a custom_field_2 value' in /var/www/clients/client125/web177/web/test/add_contact.php:89 Stack trace: #0 {main}

 

Thats the message that appears when the exception comes out. Its just in red. Is there a portion of the code that I could edit to make a nice looking box etc.

Developer Portal

View API documentation, code samples, get your API key.

Visit Page

Constant Contact 2020 End of Summer Community Sweepstakes!

The Constant Contact User Community is hosting a sweepstakes. The more you participate, the more chances you have to win! Read on to learn more...

Read More
Featured