0

我得到了这样的表格:

<input name="form[AttendeesInstitution][]" type="text">
<input name="form[AttendeesInstitution][]" type="text">

使用 Firefox 开发人员工具时,我可以从我的 $_POST 中看到我得到了一个有效的值数组。我的问题是它被提交到像“Flexsus Brugsen”这样的数据库。我需要它是像这样的逗号分隔值:“Flexsus,Brugsen”。我试图在一个有限的 Joomla 组件中构建它,所以我可以在 _POST 之前做一些 jQuery 或在 _POST 上做一些 PHP。但我找不到解决方案:-(

在此处输入图像描述

在此处输入图像描述

编辑:删除 HTML id

4

4 回答 4

0

请为每个元素设置不同的 ID,之后您将能够使用此代码。

这是你如何做到的

<?php
     $post = array(); 
     foreach($_POST as $key=>$value){
         $post[$key]= $post[$key].', '.$value;
     }
     /* Now $post will look as what you want*/
 ?>
于 2018-06-07T14:06:53.530 回答
0

在你的 joomla 控制器中,你应该得到这样的数据:

$formData = $this->input->post->get('form', array(), 'array');
$attendeesInstitution = implode(',', $formData['AttendeesInstitution']);
于 2018-06-08T06:30:50.267 回答
0

我认为您假设$_POST设置为

$_POST["form"]["AttendeesInstitution"][]

实际上,我会想象它的设置是这样的(我需要看到输出print_r($_POST)才能确定)。

$_POST["form[AttendeesInstitution]"][]

所以你访问它的方式应该是抛出一个Undefined index

你有两种选择来解决这个问题

像你有它一样循环数组,但可能不是你想要的:

foreach($_POST["form[AttendeesInstitution]"] as $k=>$v){
    //Stuff you do here
}

或更新您的表单输入以按照您想要的方式制作数据,因为$_POST它已经是一个数组。

<input name="[form][AttendeesInstitution][]" type="text">
<input name="[form][AttendeesInstitution][]" type="text">

我也不能 100% 测试这个。

于 2018-06-07T16:31:22.550 回答
0

您可以使用 php implode() 函数

<?php
...
implode(",",$_POST["form"]["AttendeesInstitution"]);
...
?>
于 2018-06-07T14:27:08.783 回答