如何处理查询字符串中的复选框值?

时间:2017-10-18 14:08:54

标签: php ajax hubspot

我有一个查询字符串,其中包含一堆表单字段值。其中一个字段是复选框,它会创建重复的参数,例如:

  

employee_selection_needs =猫&安培; employee_selection_needs =狗&安培; employee_selection_needs =猪

我需要将所有这些复选框值放入一个变量中以提交给HubSpot API。我怎么能做到这一点?我用foreach循环尝试了它,但这似乎不起作用。

$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

2 个答案:

答案 0 :(得分:0)

为了传递具有相同名称的多个参数,您应该在名称的末尾包含[]。所以你的网址应该是这样的:

  ?

employee_selection_needs [] =猫&安培; employee_selection_needs [] =狗&安培; employee_selection_needs [] =猪

要执行此操作,您的复选框应

name="employee_selection_needs[]"

答案 1 :(得分:0)

嘿,伙计!我会尽可能轻松地回答你

方法1:从查询字符串(您的案例)

  

使用?employee_selection_needs = cat& employee_selection_needs = dog& employee_selection_needs = pig PHP解析器只考虑最后一个Key:Value(pig)为此你没有一个要迭代的数组

它'拆分URL所必需的。见下文

<?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 */

?>

方法2:从HTML表单

您需要使用&#34; employee_selection_needs []&#34;而不是&#34; employee_selection_needs&#34;在复选框上。见下文

<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>

提交您将拥有一个值数组而不是单值

<?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 */

?>