I have some JS which allows me to add as many fields as I like to a form, which then when submitted, has to be saved in a database like this:
title[1] >> video_id[1] ||| title[2] >> video_id[2] ||| ...
What I need is a PHP code which allows me to get all those and put them together like above.
(Note that the number of values can between 1 and 100), and I need to gather all of them together in a string, no matter how many fields there are in the form. The current code I have can't accomplish this because it has a limited number:
$video = $_POST['title[1]'].' >> '.$_POST['video_id[1]'].' ||| '.$_POST['title[2]'].' >> '.$_POST['video_id[2]'].' ||| '.$_POST['title[3]'].' >> '.$_POST['video_id[3]'].' ||| ';
How can I do it?
I believe this should work. First, we create to arrays, one for the title
s, one for the video_id
s. Then, we concatenate the two together using >>
and |||
.
$titles = $_POST['title'];
$video_ids = $_POST['video_id'];
$video = "";
for ($i = 0, $l = count($titles); $i < $l; $i++) {
$video .= "{$titles[$i} >> {$video_ids[$i]} |||";
}
Assuming that there are as many title
s than there are video_id
s, maybe something like this could point you into the right direction:
// Count the number of elements there are in the form
// (you want to do this outside of the for loop so it doesn't
// repeat the calculation each iteration)
$elementCount = count($_POST['title']);
$video = '';
for ($i = 1; $i <= $elementCount; $i++) {
// Concatenate $_POST['title[i]'] >> $_POST['video_id[i]'] |||
// to $video
$video .= $_POST['title[' . $i . ']'] . ' >> '
. $_POST['video_id[' . $i . ']'] . ' ||| ';
}
echo $video;
I understand you are asking a question on how to pass multi-dimensional data structures from frontend to PHP backend. You have at least two choices.
PHP is able to interpret the following structure (one parameter per line, for simplicity):
items[0][title]=Movie1
items[0][video_id]=3123
items[1][title]=Movie2
items[1][video_id]=5422
and when passed as POST
body or within GET
query parameters, it will be interpreted as if passed structure would be:
$data = array(
'items' => array(
array(
'title' => 'Movie1',
'video_id' => '3123',
),
array(
'title' => 'Movie2',
'video_id' => '5422',
),
),
);
except you can simply retrieve it by doing $data = $_GET
(when passing as query params) or $data = $_POST
(when passing in request body). That is a very simple example.
To pass it from HTML, just name fields properly (in the first list I gave, names of the fields are on the left of "=
", values are on the right). So to pass the above example values, form would look like:
<form action="/some/url" method="post">
<input name="items[0][title]" value="Movie1" />
<input name="items[0][video_id]" value="3123" />
<input name="items[1][title]" value="Movie2" />
<input name="items[1][video_id]" value="5422" />
</form>
Based on similar pattern, you can create form with hidden fields (so the user won't see them), and you can construct names in similar manner (so the data structure will be maintained).
JSON is very popular format and if you are able to pass text to your backend, you can serialize any primitive data (meaning: standard objects, arrays, strings, booleans, integers, floats and null) and decode it in the backend like that:
$data = json_decode($_POST['data']); // assuming 'data' key contains JSON string
Did that answer your question?