0

I have a query string that has a bunch of form field values in it. One of the fields is a checkbox and it creates duplicate parameters, for example:

?employee_selection_needs=cat&employee_selection_needs=dog&employee_selection_needs=pig

I need to place all of these checkbox values into one variable to be submitted to the HubSpot API. How can I accomplish that? I've tried it with a foreach loop but that does not seem to work.

$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$email = $_POST["email"];
$organization_name = $_POST["organization_name"];
$phone = $_POST["phone"];
$best_describes_org = $_POST["best_describes_org"];
$organizations_safety = $_POST["organizations_safety"];
$confident_interview_process = $_POST["confident_interview_process"];
$needs_around_leadership = $_POST["needs_around_leadership"];
$used_employee_assessments = $_POST["used_employee_assessments"];
$hire_annually = $_POST["hire_annually"];

foreach($_POST['employee_selection_needs'] as $needs) {
    $employee_selection_needs .= $needs;
}



//Need to populate these variable with values from the form.
$str_post = "firstname=" . urlencode($firstname) 
    . "&lastname=" . urlencode($lastname) 
    . "&email=" . urlencode($email) 
    . "&phone=" . urlencode($phonenumber) 
    . "&organization_name=" . urlencode($organization_name) 
    . "&best_describes_org=" . urlencode($best_describes_org) 
    . "&employee_selection_needs=" . urlencode($employee_selection_needs) 
    . "&organizations_safety=" . urlencode($organizations_safety) 
    . "&confident_interview_process=" . urlencode($confident_interview_process) 
    . "&needs_around_leadership=" . urlencode($needs_around_leadership) 
    . "&used_employee_assessments=" . urlencode($used_employee_assessments) 
    . "&hire_annually=" . urlencode($hire_annually) 
    . "&hs_context=" . urlencode($hs_context_json); //Leave this one be
Unamata Sanatarai
  • 6,475
  • 3
  • 29
  • 51
Dustin
  • 4,314
  • 12
  • 53
  • 91

2 Answers2

0

In order to pass multiple parameters with the same name you should include [] in the end of the name. So your url should look like this:

?employee_selection_needs[]=cat&employee_selection_needs[]=dog&employee_selection_needs[]=pig

To do this your checkbox should have

name="employee_selection_needs[]"
Christian Giupponi
  • 7,408
  • 11
  • 68
  • 113
  • Yeah.. this is HubSpot though and I can't add [] to the name :( – Dustin Oct 18 '17 at 14:26
  • Is there some official doc for that? – Kirby Dec 07 '21 at 14:00
  • from my understanding there is no standardized method to pass an array in url parameters, this method is applicable for php and even there, there is no mention in the manual (that I could find at least) except if you look at how `http_build_query` is working – Dimitris Filippou Dec 07 '21 at 15:40
0

Hei man! I'll try to answer You as easily as possible


Method 1 : From Query String (Your case)

Using ?employee_selection_needs=cat&employee_selection_needs=dog&employee_selection_needs=pig the PHP parser consider only last Key:Value (pig) and for this You haven't an Array to iterate

It' necessary to split URL. See below

<?php

/** CODE */

/** Create an Array of Query Strings Values 
  *
  * OUTPUT EXAMPLE
  * array( 
  *     0 => 'employee_selection_needs=cat',
  *     1 => 'employee_selection_needs=dog',
  *     2 => 'employee_selection_needs=pig',
  *     3 => 'other_key=other_value',
  *     ...
  * )
  */
$queryArray= explode('&', $_SERVER['QUERY_STRING']);

/** Init a variable for append Query Values */
$myOutputString = "";

/** Need for separate append values */
$myOutputToken = "|";

/** Append to $myOutputString the correct Query Value */
foreach($queryArray as $queryArrayElement) {
    /** Create an Array starting from a Query String
      *
      * OUTPUT EXAMPLE
      * array( 
      *     0 => 'employee_selection_needs',
      *     1 => 'cat',   
      * )
      */
    $tArray= explode('=', $queryArrayElement);

    /** Filter only "employee_selection_needs" key and append Query Value
      * to $myOutputString 
      */
    if(!empty($tArray[0]) && $tArray[0] == 'employee_selection_needs') {
        if(!empty($tArray[1])) 
            $myOutputString .= $tArray[1].$myOutputToken;
    }
}

/** Remove last Token */
if(!empty($myOutputString))
    $myOutputString = substr($myOutputString, 0, -strlen($myOutputToken));

print_r($myOutputString); // Expected result cat|dog|pig

/** OTHER APPEND HERE */

?>

Method 2 : From HTML Form

You need to use "employee_selection_needs[]" instead of "employee_selection_needs" on checkbox. See below

<form ...>
    <input type="checkbox" value="dog" name="employee_selection_needs[]" />
    <input type="checkbox" value="cat" name="employee_selection_needs[]" />
    <input type="checkbox" value="fish" name="employee_selection_needs[]" />
    ...
</form>

On submit You'll have an Array of values instead Single value

<?php

/** ... CODE ... */

/** Now is an array =)) */
$used_employee_assessments = $_POST["used_employee_assessments"];

/** ... CODE ... */

/** Convert to String, if You need */
$myOutputString = "";
foreach($used_employee_assessments as $assessment) 
    $myOutputString .= $assessment;

/** OR use implode ( string $glue , array $pieces )
  * $myOutputString = implode(" %MY_TOKEN% ", $used_employee_assessments);
  */

/** OTHER APPEND HERE */

?>
Community
  • 1
  • 1