通过PHP获取数组中所有POST复选框的值?

时间:2016-05-23 20:29:26

标签: php arrays post checkbox

我正在制作一个表单,允许用户检查他们想要某些推送通知的位置。有多个复选框在PHP中进行POST。这是我的PHP代码段:

if(isset($_POST['Submit'])) {
function sendMessage(){
    $pushmessage = ($_POST['PushMessage']);
    $content = array(
    "en" => $pushmessage
    );

foreach($_POST['formDeliver'] as $check) {
        $segments = $check; 
    }

$fields = array(
  'app_id' => "app-id-here",
  'included_segments' => array($segments),
  'data' => array("foo" => "bar"),
  'contents' => $content
);

编辑:这是我的复选框代码:

      <input class="LCB" type="checkbox" id="district" value="All" name="formDeliver[]" checked><label class="light" for="district">District-Wide (Everyone)</label><br>
      <input class="LCB" type="checkbox" id="high" value="High School" name="formDeliver[]"><label class="light" for="high">High School</label><br>
      <input class="LCB" type="checkbox" id="middle" value="Middle School" name="formDeliver[]"><label class="light" for="middle">Middle School</label><br>
      <input class="LCB" type="checkbox" id="williams" value="Williams Ave." name="formDeliver[]"><label class="light" for="williams">Williams Ave.</label><br>
      <input class="LCB" type="checkbox" id="norwoodv" value="Norwood View" name="formDeliver[]"><label class="light" for="norwoodv">Norwood View</label><br>
      <input class="LCB" type="checkbox" id="sharpsburg" value="Sharpsburg Elementary" name="formDeliver[]"><label class="light" for="sharpsburg">Sharpsburg Ave.</label><br>
      <input class="LCB" type="checkbox" id="athletics" value="Athletics" name="formDeliver[]"><label class="light" for="athletics">Athletics</label>

每当提交表单时,$ fields数组都会提交消息信息。我需要将段(复选框的值)作为字符串传递:&#34; String 1&#34;,&#34; String 2&#34;,&#34; String 3&#34;。但是,每当我这样做时,它只会抓取最后一个勾选的复选框并将其提交到$ segments变量中。这是我做错了还是看不到的东西?

2 个答案:

答案 0 :(得分:2)

忘记foreach()每次覆盖$segments变量。只需使用复选框数组:

根据评论,您可能需要JSON

'included_segments' => json_encode($_POST['formDeliver']),

以逗号分隔的列表:

'included_segments' => implode(', ', $_POST['formDeliver']),

在您当前的代码中,这可行,但是多余而且不需要:

foreach($_POST['formDeliver'] as $check) {
    $segments[] = $check; 
}
$segments = implode(', ', $segments);
//or
$segments = json_encode($segments);

答案 1 :(得分:0)

由于您知道每个checkbox的具体名称,因此您可以手动创建一个阵列,如果选中了特定TRUE,则会保存checkbox,如果不是FALSE,则可以T:

$form_checks = [];//creating an array that will save the info obtained

//saving TRUE or FALSE for each specific checkbox
$form_checks['All'] = (isset($_POST['All']))?TRUE:FALSE;
$form_checks['High School'] = (isset($_POST['High School']))?TRUE:FALSE;
$form_checks['Middle School'] = (isset($_POST['Middle School']))?TRUE:FALSE;
$form_checks['Williams Ave.'] = (isset($_POST['Williams Ave.']))?TRUE:FALSE;
$form_checks['Norwood View'] = (isset($_POST['Norwood View']))?TRUE:FALSE;
$form_checks['Sharpsburg Elementary'] = (isset($_POST['Sharpsburg Elementary']))?TRUE:FALSE;
$form_checks['Athletics'] = (isset($_POST['Athletics']))?TRUE:FALSE;

//inserting the result into your $fields array:
$fields = array(
  'app_id' => "app-id-here",
  'included_segments' => $form_checks,
  'data' => array("foo" => "bar"),
  'contents' => $content
);

如果您有一个包含每个checkbox名称的数组,您还可以创建一个foreach循环来以类似的方式为您执行此操作。此外,如果您不是专门寻找TRUEFALSE,则可以针对您偏好的任何值进行更改,可能是"CHECKED""NOT-CHECKED"