0

I got a form like this:

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

When using Firefox Developer Tools I can see from my $_POST that I got a working array of values. My problem is that it is submitted to database like "Flexsus Brugsen". I need it to be comma separated values like this: "Flexsus, Brugsen". I'm trying to build this in a limited Joomla component, so I can either do some jQuery just before _POST or some PHP on _POST. But I can't find a solution :-(

enter image description here

enter image description here

EDIT: Removed HTML id's

Henrik
  • 45
  • 10
  • 3
    You cannot have elements with the same id as an FYI. – nerdlyist Jun 07 '18 at 13:58
  • First thing - ids should be unique (perhaps a typo) . – CSSBurner Jun 07 '18 at 13:58
  • Second thing - the issue then is with your backend processing script, because the data looks to be submitted properly to the backend. Can we please see your backend processing script? – CSSBurner Jun 07 '18 at 13:59
  • I got inspiration from this: https://stackoverflow.com/questions/20184670/html-php-form-input-as-array answer by @Hanky Panky. And it is working just fine and submitted to Joomla like: "firstvalue secondvalue". So I don't understand what you mean with cannot have same IDs. – Henrik Jun 07 '18 at 14:07
  • They mean a HTML `id` attribute like this `id="AttendeesInstitution"` should be unique to the whole page. Otherwise js code will not work properly if you attempt to use a duplicated `id` – RiggsFolly Jun 07 '18 at 14:10
  • Can you get any values? When in the PHP code can you do a `print_r($_POST)` and give us the output? – nerdlyist Jun 07 '18 at 16:18

4 Answers4

0

Please set different ID for each element and after that you will be able to use this code.

Here is how you can do it

<?php
     $post = array(); 
     foreach($_POST as $key=>$value){
         $post[$key]= $post[$key].', '.$value;
     }
     /* Now $post will look as what you want*/
 ?>
Bellash
  • 7,560
  • 6
  • 53
  • 86
0

You can use php implode() function

<?php
...
implode(",",$_POST["form"]["AttendeesInstitution"]);
...
?>
Davit Huroyan
  • 302
  • 4
  • 16
0

I think you are assuming that $_POST is being set like

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

In actuality I would imagine it is being set like (I would need to see the output of print_r($_POST) to be certain).

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

So the way you are accessing it should be throwing an Undefined index

You have two options to fix this

Loop the array like you have it but probably not what you want:

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

or update your forms input to make the data the way you want because $_POST is already an array.

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

I cannot 100% test this either.

nerdlyist
  • 2,842
  • 2
  • 20
  • 32
  • I tried to change name attribute of my input, then nothing was saved to database. So I don't think I can change that. I guess SQL code of Joomla component looks for a name like started in my question. So I think the loop is the answer. But my trying to implode it does not work. My full form can be found [here](http://shop.kbhmadhus.dk/kurser/tilmeld/1160-nordic-assembly-2018). In the grey section you can click "Add another", this is giving the multiple input array fields for companyname. I need to be able to separate the participants by comma. It acctually goes for all the fields in this form. – Henrik Jun 07 '18 at 19:01
  • @Henrik If you are using Joomla you should tag your question appropriately. What about the other way of parsing the `$_POST` I gave? If that does not work then can you do a print of what post looks like? There is not enough information here to help. – nerdlyist Jun 07 '18 at 20:27
  • Yes, I might use your parsing $_POST. But I maybe implode it wrong. My POST looks like the first image I attached. 0:Flexsus 1:Brugsen. That is the params in my array. I don't understand way I have this problems adding a ', ' to the input value. It should be simple to do in the foreach loop. – Henrik Jun 07 '18 at 20:53
  • @Henrik assuming that you are loop correctly what is the output of `$post` when you are done? FYI a `print_r($_POST)` is more familiar to a lot of us for debugging and helping us only helps you. – nerdlyist Jun 07 '18 at 23:44
0

in your joomla controller, you should get the data like this:

$formData = $this->input->post->get('form', array(), 'array');
$attendeesInstitution = implode(',', $formData['AttendeesInstitution']);
JulienV
  • 775
  • 7
  • 12