2017-10-18 84 views
0

我有一個查詢字符串中有一堆表單字段值。其中一個字段是一個複選框,並創建重複的參數,例如:如何處理查詢字符串中的複選框值?

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 

回答

0

爲了傳遞多個參數具有相同的名稱,你應該在名稱中包含結束[]。所以您的網址應該是這樣的:?

employee_selection_needs [] =貓& employee_selection_needs [] =狗& employee_selection_needs [] =豬

要做到這一點您的複選框應該有

name="employee_selection_needs[]" 
+0

是啊..這是HubSpot,雖然,我不能[]添加名稱:( – Dustin

0

嘿!我會嘗試儘可能方便地


方法1回答你:?從查詢字符串(你的情況)

使用employee_selection_needs =貓& employee_selection_needs =狗& employee_selection_needs =豬PHP解析器只考慮最後一個鍵:值(豬),爲此您沒有要迭代的陣列

'必須拆分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表單

您需要在複選框使用 「employee_selection_needs []」 而不是 「employee_selection_needs」。見下面

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

在提交你得值而不是單值的Array

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

?> 
相關問題