Concatenate selects options and checkboxes to one value separated by "," - PHP - SitePoint Forums | Web Development & Design Community

2022-09-09 19:13:37 By : Ms. Anny Ren

Hello, I’m trying to concatenate I think they call it and it seems I’m not doing it correctly.

I have several selects and you choose options, I would like to make them go as one value to databa base separated by “,” at the same time you can add using javascript more rows that have the same selects and checkboxes so you can add more users to the form at the same time but this ones go in another row.

It submites says success, etc. Then I go to the database but I only see that one option has been recorded.

I want to go to database and see Option 2, Option 10, Option 12, whatever. All as one value.

What I’m I doing wrong. Thanks

I’m going to start off by telling you that the database guys are going to wander into this thread and tell you what you’re doing wrong is putting all the option choices together in a comma separated string in a single field.

Secondly, this isnt what you want. I can already tell you this isnt what you want. But we’ll go ahead with it for now, and you can come back and ask for what you actually want later

Right. Now that that’s out of the way, how do you get what you asked for.

This can be simplified; implode can do all this in a single command.

$stmt_add->execute(array($useremail[$i],$section[$i]));

$section isnt an array anymore, it’s a string. you should just have $section here, not $section[$i].

As pointed out in your previous thread, to get the select/option field data to match up with the corresponding email field data, you must use a two dimensional array name for the select/option field and the 1st array index must be set, starting at zero, to match the implied index value for the email field.

For the posted markup, this would look like -

For the next dynamically added section, the index value would be 1, …

Note: for the required attribute to do anything useful for a select/option menu, the first choice must have an empty value - value="" attribute.

If you then look at the submitted post data using - echo '<pre>'; print_r($_POST); echo '</pre>'; you can see what it is.

You would access the corresponding data for the fields in each set using -

And lastly, you would not store the section values as a delimited list in a single column. This makes code and queries for every operation on that data more complicated. You would store the section data, one row per data item in a ‘section’ table, related back to the parent table through the parent’s id (autoincrement primary index.)

I would suggest you take a look at JSON. It is really easy to understand and it will be the perfect solution for your problem…

Thanks a lot of your answers and time, at the end I’ve decided to just add one more column in my database, I already made that decision before making the question since I can still print the output in the html like I want to. But I was trying and not getting results and I really just wanted to know how to do it if I weren’t adding a new column, I won’t use the answers in this particular form, but ill use them and work with them to learn more thats for sure.

Hehe. That was the outcome I expected tbh.

I would actually have modified @mabismad’s code above personally, and put the section indicies to be 0 and 1 respectively (or given them two different names), so that you can index email, the first selection, and the second selection all by the same value.

Powered by Discourse, best viewed with JavaScript enabled