Assorted Value [] equals checkbox

1

I made a table with 180 checkboxes but only 3 will be randomly selected by the site visitor. When I click "Next" it will appear OK in the URL of the next page:

....&values[]=18&values[]=19&values[]=110

On the next page how do I capture these values and resubmit the form to re-appear in the URL?

I read a question here from the site that this would be here but I do not know if it's right or if I put it in the right place because nothing has changed.

if( !empty( $_GET['values'] ) ) { foreach( $_GET['values'] as $key => $value ) { echo "<br />Semana $key<br />"; foreach( $value as $values ) { echo "$values<br/>"; } } } ?>

    
asked by anonymous 10.04.2016 / 02:10

1 answer

0

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 far so good.

The data arrived in PHP in the variable GET and became this way

Array
(
    [values] => Array
        (
            [0] => 18
            [1] => 19
            [2] => 110
        )
)

We have some problems in foreach that you did, they are:

  • 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 match any week. They are generated incrementally starting at 0, unless you change it.
  • You have created a second foreach bound to the first, but the $ value variable corresponding to the first foreach is not an array, not your case.

In order to work, you should do it this way (besides correcting the question of the week, it depends on what you want 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 steps in which the visitor is filling in the data and moving forward, so in order to use the data of the first form in the future, you must store them in some way.

There are many ways to do this, it all depends on your system, features and how secure it needs to be.

Two of the most common and simple forms are:

  • You can store the data in the database, retrieving it through the user's session ID (if you have a login system and it is logged in) or through a session identifier you created 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 certain session, besides having to have the cookie with the session ID, the client must also have the same IP used to create the session. It is good to also put an expiration date on the data).
  • It is possible to store data using PHP only through sessions or cookies, which depending on the system is not recommended, but is the easiest way to do it.

I'll give you an example using just cookies, but it's pretty much the same for sessions. 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 will be mounted called first_home with value 0=18&1=19&2=110

To access the content of the cookie later use $_COOKIE['primeira_etapa'] and remember that it will only be available on the next reload of the site.

Recommended Readings

Function print_r
Operator Ternary
Foreach > http_build_query function
Working with cookies in PHP

    
10.04.2016 / 06:08