首页 > 解决方案 > 将变量从一个 php 文件发送到另一个文件并在第三个文件中显示变量值

问题描述

bookincome.php 接受几个输入,bi.php 根据从 bookincome.php 获取的值计算总净收入,并显示一个表格,其中包含来自用户的所有给定值和计算的总账面收入 ($tnibeforetax)价值。现在我的report1.php 中有一个表,我想在其中显示计算的$tnibeforetax 值,但我无法在report1.php 中显示该表中的值。请帮忙

bookincome.php 的代码:

<?php
// start session
session_start();
?>
  <!DOCTYPE html>
  <html>

  <head>
    <title> Book Income </title>
  </head>

  <body>
    <center>
      <h2>BOOK INCOME AS PER TRIAL BALANCE ENTRY </h3>
    </center>
    <form name='form' action="bi.php" method='post' autocomplete='off' target="_blank" [....]>
      <table cellspacing="10">
        <tr>
          <br>
          <td align="left">Book Income as per Trial Balance: </td>
          <td><input type="number" name="bi" value="<?php if(isset($_POST['bi'])) ?>"></td>
        </tr>
        <tr>
          <br>
          <td align="left">Late Adjustment 1: </td>
          <td><input type="number" name="la1" value="<?php if(isset($_POST['la1'])) ?>"></td>
        </tr>
        <tr>
          <br>
          <td align="left">Late Adjustment 2: </td>
          <td><input type="number" name="la2" value="<?php if(isset($_POST['la2'])) ?>"><br> </td>
        </tr>
        <tr>
          <br>
          <td align="left">Late Adjustment 3: </td>
          <td><input type="number" name="la3" value="<?php if(isset($_POST['la3'])) ?>"><br> </td>
        </tr>
        <tr>
          <br>
          <td align="left">Audit Adjustment 1: </td>
          <td><input type="number" name="aa1" value="<?php if(isset($_POST['aa1'])) ?>"><br> </td>
        </tr>
        <tr>
          <br>
          <td align="left">Audit Adjustment 2: </td>
          <td><input type="number" name="aa2" value="<?php if(isset($_POST['aa2'])) ?>"><br> </td>
        </tr>
        <tr>
          <br>
          <td align="left">Audit Adjustment 3: </td>
          <td><input type="number" name="aa3" value="<?php if(isset($_POST['aa4'])) ?>"><br> </td>
        </tr>

      </table>
      <br>
      <center> <button type="submit" value="Submit">Submit</button>
        <button type="reset" value="Reset">Reset</button> </center>
    </form>
  </body>

  </html>

以下是bi.php的代码:

<?php
// start session
session_start();

			$bi = $_POST['bi'];
			$la1 = $_POST['la1'];
			$la2 = $_POST['la2'];
			$la3 = $_POST['la3'];
			$aa1 = $_POST['aa1'];
			$aa2 = $_POST['aa2'];
			$aa3 = $_POST['aa3'];
			
			$tnibeforetax= $bi + $la1 + $la2 + $la3 + $aa1 + $aa2 + $aa3;
			
			$_SESSION['tnibeforetax'] = $tnibeforetax; 
				
?>
  <html>

  <head>
    <style>
      table,
      th,
      td {
        border: 1px solid black;
        border-collapse: collapse;
      }
    </style>
  </head>

  <body>

    <table style="width:50%" align="center">
      <tr>
        <td width="70%">&nbsp;</td>
        <td align="center"> Amount </td>
      </tr>
      <tr>
        <td> Book Income as per trail Balance </td>
        <td align="right">
          <?php echo $bi ; ?> </td>
      </tr>
      <?php 
		if($la1!=0)
		{
			echo '<tr><td> Late Adjustment1 </td> ';
			echo '<td align="right">'. $la1 . '</td><tr>';
		}
	?>
      <?php 
		if($la2!=0)
		{
			echo '<tr><td> Late Adjustment2 </td>';
			echo '<td align="right">'. $la2 . '</td></tr>';
		}
	?>
      <?php 
		if($la3!=0)
		{
			echo '<tr><td> Late Adjustment3 </td> ';
			echo '<td align="right">'. $la3 . '</td></tr>';
		}
	?>
      <?php 
		if($aa1!=0)
		{
			echo '<tr><td> Audit Adjustment1 </td> ';
			echo '<td align="right">'. $aa1 . '</td></tr>';
		}
	?>
      <?php 
		if($aa2!=0)
		{
			echo '<tr><td> Audit Adjustment2 </td></tr> ';
			echo '<tr><td align="right">'. $aa2 . '</td></tr>';
		}
	?>
      <?php 
		if($aa3!=0)
		{
			echo '<tr><td> Audit Adjustment3 </td>';
			echo '<td align="right">'. $aa3 . '</td></tr>';
		}
	?>
      <tr>
        <td> Total Net Income before tax as per Income Statement </td>
        <td align="right">
          <?php echo $tnibeforetax ; ?> </td>
      </tr>

report1.php 的代码:

<?php
// start session
session_start();

$tnibeforetax = $_SESSION['$tnibeforetax'];

?>
<!DOCTYPE html>
<html>
<head>
<style>
	table, th, td 
	{
		border: 1px solid black;
		border-collapse: collapse;
	}
</style>
</head>
<body>

<table style="width:50%" align="center" >
	<tr>
		<td style="font-weight:bold" colspan="2" ><font color="darkgreen" > Income from Business & Proffession:</td></font>
	</tr>
	<tr>
		<td> Net Income before tax as per Income Statement </td>
		<td width="25%"> <?php echo "$" . $tnibeforetax; ?></td>
	</tr>

标签: phphtml

解决方案


它不工作的原因是因为你设置了会话值tnibeforetax但获取它$tnibeforetax请在你的第三个文件中替换上面的代码,即report1.php.

 <?php
    // start session
    session_start();

    $tnibeforetax = $_SESSION['tnibeforetax'];

    ?>

推荐阅读