Identifying and solving problems
You are passing the data via GET, with input with the same name, but with array initializer defined in the Names, so so far so good.
The data arrived at PHP in the GET variable and stayed that way
Array
(
[values] => Array
(
[0] => 18
[1] => 19
[2] => 110
)
)
We have some problems in the foreach that you made, are they:
- You set an echo for something related to the week in the $key variable inside the foreach, but as you can see in the illustration above, the array keys do not correspond to any week. They are generated increasingly starting from 0, unless you change that.
- You created a second foreach coupled to the first, but the $value variable corresponding to the first foreach, is not an array, not in your case.
To make it work, you must do it this way (besides correcting the issue of the week, it depends on what you are wanting to do)
$formValues = isset($_GET['values']) ? $_GET['values'] : null;
if (!empty($formValues)) {
foreach ($formValues as $key => $value) {
echo "Semana {$key}<br>{$value}<br>";
}
}
Storing data
You said that your system has several forms that constitute in stages where the visitor is filling in the data and moving forward, so in order to use the data of the first forms in the future, you must store them in some way.
There are many and many ways to do this, it all depends on your system, resources and how secure it needs to be.
Two of the most common and simple forms are:
- You can store the data in the database by rescuing it through the user’s session ID (if you have a login system and it is logged in) or through a session identifier created by yourself. A cookie with a unique ID, for example (in case you want to use cookies, remember to make all the necessary validations to make the system safe, for example, to access the data of a particular session, in addition to having the cookie with the session ID, the client must also have the same IP used to create the session. It is good to put also an expiration time for the data).
- It is possible to store the data using only PHP via Sessions or cookies, which depending on the system, is not so recommended, however, it is the simplest way to do.
I’ll give you an example using only cookies, but it’s pretty much the same for Sesssions. In the case of database storage, just follow the logic I gave above.
$formValues = isset($_GET['values']) ? $_GET['values'] : null;
if (!empty($formValues)) {
$formValues = http_build_query($formValues);
}
setcookie('primeira_etapa', $formValues, (time() + (30 * 60))); //30 minutos de validade
A cookie called first step with value will be set 0=18&1=19&2=110
To access the cookie content later use $_COOKIE['primeira_etapa']
and remember that it will only be available on the next Load site.
Recommended readings
Print_r function
Ternary Operator
Foreach
Function http_build_query
Working with cookies in PHP