Several equal checkbox Value[]

Asked

Viewed 82 times

1

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

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

On the next page as I do p/ capture these values and resend through the form to reappear in the URL?

I read a question here from the site that would be this but I don’t 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/>"; } } } ?>

1 answer

1

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.