[英]HTML/PHP - Form input as array, need comma separated values
我得到这样的表格:
<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
请为每个元素设置不同的ID,之后您将可以使用此代码。
这是你怎么做
<?php
$post = array();
foreach($_POST as $key=>$value){
$post[$key]= $post[$key].', '.$value;
}
/* Now $post will look as what you want*/
?>
您可以使用php implode()函数
<?php
...
implode(",",$_POST["form"]["AttendeesInstitution"]);
...
?>
我认为您假设$_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%测试。
在joomla控制器中,您应该获得如下数据:
$formData = $this->input->post->get('form', array(), 'array');
$attendeesInstitution = implode(',', $formData['AttendeesInstitution']);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.