cancel
Showing results for 
Search instead for 
Did you mean: 

Checkboxes...what the hey?

Highlighted
Occasional Participant

Checkboxes...what the hey?

I have a custom signup form here: www.pbigordon.net/golfpanel/signup.php


I have a lot of criteria I need users to fill out. I have the radio buttons figured out. I need help with the checkboxes. I can get them all to work if I give each checkbox name a custom field name, like this:

<input type="checkbox" name="custom_field_4" value="Pesticides" id="pesticides" />


...however, in doing that I run out of custom fields to give the fileld names to. 


If you look at the form I've done the method above to all the checkboxes all the way down to the "Cool Season" checkboxes. 


Since I'm running out of custom fields, for the "Warm Season" grasses I tried to set them up as checkbox groups, like this:

<p> <input type="checkbox" name="custom_field_13" maxlength="100"  value="Bahia Grass" id="custom_field_13_0" /> Bahia grass</p>

<p> <input type="checkbox" name="custom_field_13" maxlength="100"  value="Buffalograss" id="custom_field_13_1" /> Buffalograss</p>


My problem lies in the fact that this returns the value of only a single checkbox...and not all of them together. Hence my question... How do you accomplish sending two checked values to the custom field?

 

5 REPLIES 5
CTCT Employee

This is a very good question

This is a very good question and the answer relies solely on the logic of your sign-up form.


 


The process that you are using for the radio buttons is correct. Setting each option to the same name ensures that the one value selected will be passed to the appropriate custom field. With checkboxes however, you need to pass multiple values. Seeing as how you are limited to 15 custom fields, passing each option to its own custom field is not the best option.


 


So this is where the logic comes in. You have three sections of checkboxes which means we should be able to store all of this info in three custom fields: Types of Turf & Ornamental products you use, Cool Season Turf Types, and Warm Season Turf Types.


 


In HTML, setting multiple checkboxes to have the same name attribute means that all of the answers will be stored as an array with that name when it is passed to the action of the form. It is also important to note that the name must be followed by a set of open and closed square brackets like so: name="custom_field_1[]"


 


I took the liberty of rewriting one of your checkbox sections as an example:


 


  <td align="left" valign="top" bgcolor="#FFFFCC">

    <p><input name="turf_products[]" type="checkbox" value="Herb" id="herbicides" />Herbicides</p>

    <p><input type="checkbox" name="turf_products[]" value="Insect" id="insecticides" />Insecticides</p>

    <p><input type="checkbox" name="turf_products[]" value="Fung" id="fungicides" />Fungicides</p>

  </td>

  <td align="left" valign="top" bgcolor="#FFFFCC">

    <p><input type="checkbox" name="turf_products[]" value="Pest" id="pesticides" />Pesticides</p>

    <p><input type="checkbox" name="turf_products[]" value="PGR/TGR" id="pgrs" />PGR's / TGR's</p>

  </td>


 


Notice that the name is something more descriptive because as you will see in the next section, this name is not what will be passed to the API. Also notice that I have made shorthand versions of the labels for each value ie: Herbicides = Herb. The reason for this is that each custom field has a 50 character limit and while your current setup may be able to squeeze into each field, it would be better to shorten the values to save space and accommodate future changes.


 


The next step would be to create the logic to handle the new HTML form data. I am not sure how your PHP is setup to process at the top of the page however this snippet should provide you with the starting point to get this in motion:


 


   foreach ($_POST as $prod) { //Loops through the values of the turf_products array

      $custom_field_2 .= $prod.', '; //Concatenates all of the values together in a comma delimited string.

   }

   $custom_field_2 = trim($custom_field_2, ", "); //Removes the last comma


From this you can now take $custom_field_2 and pass it to the API as a single value that will look something like this: "Herb, Insect"


 


I hope that this helps you to complete your form. Feel free to reply with any other questions.

Dan H

Support Engineer, Constant Contact

Highlighted
Occasional Participant

I'm missing something...

First of all, THANK YOU! That all seems logical...now if I could just get it to work...


I did everything you said but I think I'm missing something in the PHP at the top of my page...here it is:


 <?php

include_once('cc_class.php');

$ccOBJ = new ConstantContact();

/**

 * If we have post fields means that the form have been submitted.

 * Therefore we start submiting inserted data to ConstantContact Server.

 */

    if (!empty($_POST)) {

        $postFields = array();

        $postFields = $_POST;

        $postFields = $_POST;

        $postFields = $_POST;

        $postFields = $_POST;

        $postFields = $_POST;

        $postFields= $_POST;

        $postFields = $_POST;

        $postFields = $_POST;

        $postFields = $_POST;

        $postFields = $_POST;

        $postFields = $_POST;

        $postFields = $_POST;

        $postFields = $_POST;

        // The Code is looking for a State Code For Example TX instead of Texas

        $postFields = $_POST;

        $postFields = $_POST;

        $postFields = $_POST;

        $postFields = $_POST;

        $postFields = $_POST;

        $postFields = $_POST;

        $postFields = $_POST;

        $postFields = array();

        foreach($_POST as $key=>$val) {

           

            if (strncmp($key, 'custom_field_', strlen('custom_field_')) === 0) {

                $postFields = $val;

            }



        }



        foreach ($_POST as $prod) { //Loops through the values of the turf_products array

        $custom_field_2 .= $prod.', '; //Concatenates all of the values together in a comma delimited string.

        }

        $custom_field_2 = trim($custom_field_2, ", "); //Removes the last comma



        $contactXML = $ccOBJ->createContactXML(null,$postFields);

       

        if (!$ccOBJ->addSubscriber($contactXML)) {

            $error = true;

        } else {

            $error = false;

            $_POST = array();

        }



    }



    //get all available lists for the current CC account.

    $allLists = $ccOBJ->getLists();

    //get all available states

    $allStates = $ccOBJ->getStates();

    //get all available countries

    $allCountries = $ccOBJ->getCountries();

    ?>


 


The result doesn't send any data to CC.


Also...you mention this: name="custom_field_1[]"...should I be applying this somewhere? I copy/pasted the form you did (<input type="checkbox" name="turf_products[]" value="Fung" id="fungicides2" />Fungicides</p>) and so on, and used that.

Highlighted
CTCT Employee

First let me start at the

First let me start at the bottom. The example name="custom_field_1[]" was just an example. What you have done by having name="turf_products[]" as the name for each Turf Product related option is correct.


 


Secondly you just need to move the function for concatenating the answers into the comma delimited string to within the POST processing section as seen below. Also notice that I have changed the final name of the string to be $_POST so that we can simulate that this value was sent from the form.


 


    if (!empty($_POST)) {

        $postFields = array();


        foreach ($_POST as $prod) { //Loops through the values of the turf_products array

             $custom_field_2 .= $prod.', '; //Concatenates all of the values together in a comma delimited string.

          }

          $_POST = trim($custom_field_2, ", "); //Removes the last comma




        $postFields = $_POST;

        $postFields = $_POST;

        $postFields = $_POST;

        // ... CODE REMOVED TO SHORTEN FORUM POST ... //

        $postFields = $_POST;

        $postFields = $_POST;

        $postFields = array();

        foreach($_POST as $key=>$val) {

           

            if (strncmp($key, 'custom_field_', strlen('custom_field_')) === 0) {

                $postFields = $val;

            }


        }


 Please reply back for any clarification.

Dan H

Support Engineer, Constant Contact

Highlighted
Occasional Participant

Booyah!

That worked...THAT WORKED! Thanks for taking the time to answer my question and making me look good. You have my permission to take the rest of the week off.


- Bryan

Highlighted
CTCT Employee

I am glad we got that going

I am glad we got that going for you. Ill be sure to tell my manager, too bad its already Friday though. :(

Dan H

Support Engineer, Constant Contact