How to handle checkboxes in AJAX form submission?



How should I handle fields that are checkboxes when submitting a form via AJAX? The field named “employee_selection_needs” is the one with checkboxes. I tried doing a for loop but it does not work. This is my PHP script…

$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


Hi @dmcgrew,

I’m not entirely familiar with PHP, but in order to send multiple values to a multiple checkbox property in HubSpot, the values must be concatenated in a string, separated by semicolons (e.g. If a user selects A, B, and C, the values sent to HubSpot should be “A;B;C”). Does this match the final value of $employee_selection_needs?


Ok great. That helps. Thank you!


@Derek_Gervais I just tried this but it spits out each checkbox value as one chunk. So in the HubSpot submission I actually see “A;B;C”.


Hi @dmcgrew,

Can you direct message me with a link to the contact record that the form submission was associated with?