简体   繁体   English

PHP - 将变量从一个PHP文件传递到另一个PHP文件

[英]PHP - passing variable from one PHP file to another PHP file

I am new to PHP and trying to pass variable from one page to another page. 我是PHP新手并尝试将变量从一个页面传递到另一个页面。 Initially, I have a HTML page which contains the frames as below. 最初,我有一个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.php file to my slider.php file. 如上所示,我有2个PHP页面,其中我试图将一些值从index.php文件发送到我的slider.php文件。 My index.php file is as below. 我的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.php file. 这是我的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 ". 如上所示,我试图检索名为“ totalcolumns ”的值。 However, I am not able to retrieve the value in my slider.php file. 但是,我无法检索我的slider.php文件中的值。 I also tried using SESSION as suggested in this link, but no luck. 我也尝试使用链接中建议的SESSION ,但没有运气。 Can someone please let me know what am doing wrong? 有人可以让我知道我做错了什么?

You should be able to use the $_SESSION. 您应该能够使用$ _SESSION。 This is: 这是:

$_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. 您还可以查看require或include函数。 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 你应该使用Session

http://php.net/manual/en/function.require.php http://php.net/manual/en/function.require.php

Btw, don't use framesets 顺便说一句,不要使用框架集

You should use sessions and you should not use html framesets or iframes, it is a bad practice. 你应该使用会话 ,你不应该使用HTML框架集或iframe,这是一个不好的做法。 If you don't want to reload the whole page by any change, you should use javascript. 如果您不想通过任何更改重新加载整个页面,则应使用javascript。

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 这可能对你有所帮助

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>

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

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