Using hidden "form" to pass variables

Just learning about passing variables from page to page in php, and trying to find the best way to do so for me, as I have to pass ~10 variables between 5 pages. On the first page, does it make sense to have a form:

<form method="post">
    <input type="hidden" name="test" value="<?php $test ?>" />
</form>

Then on the next page could I receive this variable using POST? I would not like to have an ACTUAL form, just use it as a storage area for my variables. Also, what do I use for action= if the second page is called second.php.

Any help is appreciated, thanks

Answers


Short Answer: Forms only work when submitted. You probably want to use sessions.

Longer Answer:

  • It does not make sense to have a form without user input. That's what it exists for.
  • the action= attribute on a form reflects where the form would be submitted. If the processing page is second.php then the action= attribute should point there.
  • Sessions are not the only possibility. PHP can also set cookies, and if the server doesn't care about the data (only being used as a medium), you can use HTML5's localStorage.

Really, if you need to be passing 10 variables through all five pages, you're probably better off using sessions. You can store all of them as part of the $_SESSION variable and access them from any page as long as the session is kept alive.


You can't use $_POST variables to store data in a user's session.

You should use:

  • Sessions
  • Cookies
  • HTML5 storage

It depends on what you want to do, you have various options: 1. Using a form and post as you outlined. In this case, on page 1 your action="second.php" 2. Passing the data via URL using GET 3. Sessions, as stated by the previous two posts 4. Cookies


If I understand your question correctly you could try the following to POST your values without having the form appear on the page or actually be on the page at all. You need to have the jQuery library referenced in order to use this code.

    function hiddenPost(param1) {
        $('<form />')
        .hide()
        .attr({ method: "post" })
        .attr({ action: "http://my-URL-here.com/SomePage.php" })
        .append($('<input />')
            .attr("type", "hidden")
            .attr({ "name": "post_data" })
            .val(param1)
        )
        .append('<input type="submit" />')
        .appendTo($("body"))
        .submit();
    }

You can retrieve the POST values on the page you POST to in the same manner as if it was a regular POST with a form tag.

For PHP: var postData = $_POST["post_data"];

Hope that helps.


Need Your Help

Using AWS S3 encryption with inputstreams

java rest amazon-web-services encryption amazon-s3

I have a rest service that streams content to a AWS S3 bucket. I was wondering what are the alternatives to encrypt that stream to the bucket, my requirements would be primary that the encryption k...

Read a CSS file outside a main folder

html css file asp-classic stylesheet

I have html file in main(X-folder) which has CSS also, and I convert HTML to asp and i need to put inside a ASP[Y] folder..