Picking up values in a $post array

Asked

Viewed 83 times

1

I have a form where I am taking the user’s choice, and inserting it into the array but it does not accumulate and only shows the current choice. How do I accumulate and every new post is added to the array?

<?php 

include "filmelistar.php";
$escolhas = array();
$filme2 = $_POST["filme2"];

$movie = $tmdb->getMovie($filme2);
// returns a Movie Object

$escolhas[] = $movie->getTitle();

echo '  <div class="panel panel-default">
            <div class="panel-body">
            <br>
                Adicionado a lista!
                <ul>';
echo $movie->getTitle();

echo '          </ul>
            </div>
        </div>';

        echo "<p>Lista para assistir:</p>";
        for ($i = 0; $i < count($escolhas); $i++) {
          echo $escolhas[$i];
          echo "<br>";
        }




?>

  • What is in the variable $tmdb? And what is your goal with that? Study?

  • It is the call of a class that is also an API of the site Themoviedatabase. My goal is to study even, I am doing a job

  • I get it. I have doubts whether it is the case to reopen the question or not, there are several ways to do this and "the best" depends on the situation. Anyway the answer below already gives you one of the alternatives (sessions). Sessions are to persist data within the same "round" (session) of use, use cases are not so many. It usually makes more sense to store data in a database.

1 answer

1


By default, each run of a PHP program starts "clean stone", virgin. To persist data across multiple client accesses, you need to create a session.

At the beginning of the program, call

session_start()

and replace the references to the "choices" matrix with

$_SESSION['escolhas']

since only the $_SESSION associative matrix is persisted between sessions, whatever you want to last, you have to stay inside.

To start the matrix the first time (when it does not yet exist in the current session) do

if (! isset($_SESSION['escolhas']))
   $_SESSION['escolhas'] = array();
}

By the way, I will describe how the session engine works. As I said before, each execution of a PHP page starts from absolute zero.

The session_start() function generates random code and inserts this code into the HTTP response header, which on the client side (Browse) is stored in a cookie.

When the same client accesses another page of the same server, the cookie is sent in the HTTP request header. The session_start() function detects this cookie, and determines that the client is already known.

The cookie only stores a "Session ID". The contents of the $_SESSION variable are stored on the server, usually in a temporary file folder (/tmp on Linux).

The customer has no way to access $_SESSION content directly, so it is considered safe to leave sensitive information on it.

Session information should be considered volatile. If something needs to persist beyond a session (for example, if a client accesses information from more than one different computer), then it needs to be stored in a database.

It is possible to store the session in a database, in which case it is not lost even if the server is reset or overwritten. This is useful, for example, when there is a cluster of servers answering for the same site, and the session should be maintained even if the client requests fall on different machines. It is also useful when the site runs inside a Docker container, as the container’s Restart would already erase all sessions if they were stored in the form of temporary files.

Sessions are an ideal mechanism for when PHP serves traditional web pages. For Webservices, one can consider some other technology like JWT.

  • Thank you for the reply, but I have made the changes yet it does not accumulate during sessions. Code: http://dontpad.com/sessionphp

  • 1

    The problem is that you are initiating $_SESSION['choices'] = array() a second time. Right after include. Remove this line.

  • @epx Thank you very much, now I understand. It’s mistake well beast my.

Browser other questions tagged

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