PHP - 将变量从一个 PHP 文件传递到另一个 PHP 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19125982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - passing variable from one PHP file to another PHP file
提问by ram esh
I am new to PHP and trying to pass variable from one page to another page. Initially, I have a HTML page which contains the frames as below.
我是 PHP 新手,并试图将变量从一个页面传递到另一个页面。最初,我有一个 HTML 页面,其中包含如下框架。
<!DOCTYPE html>
<html>
<frameset cols="70%,*">
<frame src="index.php">
<frame src="slider.php">
</frameset>
</html>
As seen above, I have 2 PHP pages, in which am trying to send some values from index.phpfile to my slider.phpfile. My index.phpfile is as below.
如上所示,我有 2 个 PHP 页面,其中我试图将一些值从index.php文件发送到我的slider.php文件。我的index.php文件如下。
<?php
$names = file('demo.csv');
$page = $_GET['page'];
$pagedResults = new Paginated($names, 20, $page);
$handle = fopen('demo.csv', 'r');
if (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
{
}
echo "<table border='3' bgcolor='#dceba9' style='float:center; margin:50'>";
echo '<tr><th>'.implode('</th><th>', $data).'</th></tr>';
while ( $row = $pagedResults->fetchPagedRow())
{
echo "<tr><td>";
$row1 = str_replace( ',', "</td><td>", $row );
echo $row1;
echo "</td></tr>";
}
fclose($handle);
echo "</table>";
//important to set the strategy to be used before a call to fetchPagedNavigation
$pagedResults->setLayout(new DoubleBarLayout());
echo $pagedResults->fetchPagedNavigation();
?>
<form method="get" action="slider.php">
<input type="hidden" name="totalcolumns" value="3">
<input type="submit">
</form>
This is my slider.phpfile.
这是我的slider.php文件。
<?php
$totalcolumns = $_GET['totalcolumns'];
echo "My next value should get printed";
echo $totalcolumns;
?>
<input type="text" data-slider="true" data-slider-range="100,500" data-slider-step="100">
</html>
As seen above, I am trying to retrieve the value with the name "totalcolumns". However, I am not able to retrieve the value in my slider.phpfile. I also tried using SESSIONas suggested in thislink, but no luck. Can someone please let me know what am doing wrong?
如上所示,我试图检索名称为“ totalcolumns”的值。但是,我无法检索我的slider.php文件中的值。我也尝试按照此链接中的建议使用SESSION,但没有成功。有人可以让我知道我做错了什么吗?
回答by fos.alex
You should be able to use the $_SESSION. This is:
您应该能够使用 $_SESSION。这是:
$_SESSION['totalcolumns'] = $columns --> your value here in the first script
your value will be stored in the $columns variable in the second
$columns = $_SESSION['totalcolumns']
You can also review the require or include functions. These functions make one file depend on another one, it is as if you directly paste one file on the other. It is not a good practice to pass variables with these functions. You should use Session
您还可以查看 require 或 include 函数。这些功能使一个文件依赖于另一个文件,就好像您直接将一个文件粘贴到另一个文件上一样。用这些函数传递变量不是一个好习惯。你应该使用会话
http://php.net/manual/en/function.require.php
http://php.net/manual/en/function.require.php
Btw, don't use framesets
顺便说一句,不要使用框架集
回答by Dinesh
You can use $_REQUEST
instead of $_GET
or just it as:
您可以$_REQUEST
代替$_GET
或仅使用它作为:
<?php
if(array_key_exists('totalcolumns', $_GET)) {
$totalcolumns = $_GET['totalcolumns'];
echo "My next value should get printed";
echo $totalcolumns;
?>
may this help you
这可以帮助你
回答by Gar
i'd id the frames first, removing the src for the second one
我首先识别帧,删除第二个的 src
<!DOCTYPE html>
<html>
<frameset cols="70%,*">
<frame src="index.php" id="f1">
<frame src="" id="f2">
</frameset>
</html>
then change the index.php adding this piece of code at the end
然后更改 index.php 在最后添加这段代码
<script>
parent.frames['f2'].location.href="slider.php?totalcolumns=3";
</script>
or perhaps if you have the totalcolumns in your php
或者如果你的 php 中有 totalcolumns
<script>
parent.frames['f2'].location.href="slider.php?totalcolumns=<?php echo $totalcolumns;?>";
</script>