简体   繁体   English

在多个页面上使用PHP变量

[英]Using PHP variables on multiple pages

Is it possible to use a variable from one page in a piece of code on another? 是否可以在另一页的代码段中使用一页中的变量? eg submit a form on one page and on the second page use a PHP script to add the data from the form to a MySQL table 例如,在一页上提交表单,然后在第二页上使用PHP脚本将表单中的数据添加到MySQL表中

Thanks for all the help 谢谢你的帮助

Your best bet would be to use PHP's session functions. 最好的选择是使用PHP的会话函数。 That way if the user navigates away from your page and then comes back, the session variables will still be there (provided the session hasn't expired). 这样,如果用户离开您的页面然后返回,则会话变量仍将存在(前提是会话尚未过期)。 You can get more information here -- PHP Sessions . 您可以在此处获得更多信息-PHP会话 Basically all you have to do is call 基本上,您要做的就是打电话

session_start();

at the top of each php page (before anything is outputted to the browser) where you want to have access to the session variables. 在每个php页面的顶部(任何内容输出到浏览器之前),您要在其中访问会话变量。 You can then set/retrieve a variable using 然后,您可以使用以下方法设置/获取变量

// set
$_SESSION['varname'] = "something";
// retrieve
$somevar = $_SESSION['varname'];

This is what the GET and POST 'super' globals are for. 这就是GET和POST'super'全局变量的用途。 http://www.tizag.com/phpT/postget.php http://www.tizag.com/phpT/postget.php

The script that generates the form does not have to be the script the processes the form. 生成表单的脚本不必是处理表单的脚本。 all it takes is for the FORM tag to point to the correct script. 只需要FORM标记指向正确的脚本即可。 The only caveat is that whatever page you submit the form data to also has to then generate some suitable output (unless you do a AJAX POST). 唯一的警告是,提交表单数据的任何页面还必须随后生成一些合适的输出(除非您执行AJAX POST)。

If you're instead asking if two page executions can handle the same POSTed data, well that is quite a different question. 相反,如果您要问两个页面执行是否可以处理相同的POSTed数据,那将是一个完全不同的问题。 At a simplistic level, you can have the first include() the second so it has access to all the same data. 在一个简单的级别上,您可以让第一个include()第二个,以便它可以访问所有相同的数据。 A bit more advanced is to re-create the original POST and synthesize a POST. 更高级的是重新创建原始POST并合成POST。 This will probably require some JavaScript assistance. 这可能需要一些JavaScript协助。 Another approach is to use PHP's session feature and put the value aside so a later script has access to it. 另一种方法是使用PHP的会话功能并将其值放在一边,以便以后的脚本可以访问它。 This relies on the user then following the correct link, however! 但是,这取决于用户,然后点击正确的链接!

Use session to access your variable to the second/other page. 使用session访问您的变量到第二个页面。 Example: 例:

index.php index.php

<?php

session_start();
$_SESSION['yourData'] = 'yourData';
?>

========================================================================= ================================================== =======================

second_page.php second_page.php

<?php
  echo $_SESSION['yourData'];
?>

You may try this one. 您可以尝试这个。 Visit this link for more details. 访问此链接以获取更多详细信息。 Or PHP's official website. 或PHP的官方网站。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM