PHP: Pass non-form variables between pages?

坚强是说给别人听的谎言 提交于 2020-01-11 12:26:53

问题


I have a page. The user submits the page and sends it to a PHP results page. It works fine. Now, I want the results page to link to another page, and for the elements on that page to depend on what was on the results page. I know how to pass form variables to another page, but I don't know anything about passing non-form variables.

From my searching on the web, my best guess is that I should be passing by URL. Is this correct? If so, a possible problem: the page I want the results page to pass to will have a form, and the user will go to yet another results page by clicking submit (the form data will be sent by POST). Can I send the non-form data (the old results page variable) along with the form data, if the user is going to the other page using POST?


回答1:


There are several options. However, the easiest may be to simply pass the data on using hidden input fields.

Other options would be using the session, storing to a database between forms, or some combination therein.




回答2:


I strongly suggest using sessions. It's not that hard to learn, php makes it VERY easy using http://php.net/session_start and the $_SESSION variable. Advantage is that you will not have to submit a form on every click, and no information will be displayed in plain text in the URL.




回答3:


If you are going to use POST to go to the next page the most simple option is to include the data you want to send along using an input type="hidden" element in your form.




回答4:


You might consider using a session to pass the data along.




回答5:


You can embed the non-form data into the second form as hidden fields. To make sure that it is also passed in the link, you can just add it to the URL as a query string, like:

http://..../blah.php?var1=val1&var2=val2

as long as the data you're passing can fit into a URL. Be sure to urlencode() anything you're appending to the URL.




回答6:


<?php

start_session();

$_SESSION['Foo'] = 'Bar' // Add stuff here.

?>


来源:https://stackoverflow.com/questions/4100450/php-pass-non-form-variables-between-pages

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!