PHP: Pass non-form variables between pages?

2019-03-01 13:23发布

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?

标签: php forms xhtml
6条回答
劳资没心,怎么记你
2楼-- · 2019-03-01 13:57

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.

查看更多
爷的心禁止访问
3楼-- · 2019-03-01 13:57

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.

查看更多
贪生不怕死
4楼-- · 2019-03-01 14:10
<?php

start_session();

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

?>
查看更多
男人必须洒脱
5楼-- · 2019-03-01 14:15

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.

查看更多
Deceive 欺骗
6楼-- · 2019-03-01 14:17

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

查看更多
看我几分像从前
7楼-- · 2019-03-01 14:17

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.

查看更多
登录 后发表回答