开发者

PHP: Pass non-form variables between pages?

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 anoth开发者_StackOverflow社区er 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?


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.


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.


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.


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


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.


<?php

start_session();

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

?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜