简体   繁体   中英

Passing PHP Variables to other pages

I have a form where a user enters details and results are shown on the same page. These results can then be submitted.

First_page.php

<?php
session_start();

if (isset($_POST['applySubmit'])) { 
    $_SESSION['amountdiv'] = $amountdiv;
    $_SESSION['fixedrate'] = $fixedrate;
    $_SESSION['deliverydiv'] = $deliverydiv;
    $_SESSION['date'] = $date;  
    $_SESSION['total'] = $total;    
    $_SESSION['grandtotal'] = $grandtotal;      
    $_SESSION['reference'] = $reference;
}
?>

I've seen a lot of examples on this and I'm using this example from this post PHP Pass variable to next page . The problem I have is that on the second page, where I want to output the details, nothing is appearing.

Second_page.php

<?php
session_start();
//*************Session form details to carry through*****************//
$amountdiv = $_SESSION['amountdiv'];
$fixedrate = $_SESSION['fixedrate'];
$deliverydiv = $_SESSION['deliverydiv'];
$date = $_SESSION['date'];  
$total = $_SESSION['total'];    
$grandtotal = $_SESSION['grandtotal'];
$reference = $_SESSION['reference'];
?> 

Then I want to echo this info out onto the screen but nothing is appearing.

<td class="blaah1"><?php echo $_SESSION['amountdiv'] ;?></td>
<td class="blaah1"><?php echo $_SESSION['grandtotal'] ;?></td>
<td class="blaah3"><?php echo $_SESSION['date'] ; ?></td>

Why isn't anything appearing?

Here :

if (isset($_POST['applySubmit'])) { 
    $_SESSION['amountdiv'] = $amountdiv;
    $_SESSION['fixedrate'] = $fixedrate;
    $_SESSION['deliverydiv'] = $deliverydiv;
    $_SESSION['date'] = $date;  
    $_SESSION['total'] = $total;    
    $_SESSION['grandtotal'] = $grandtotal;      
    $_SESSION['reference'] = $reference;
}

Where are you assigning values for these variables : $amountdiv , $fixedrate ... All vars looks null.

Second, check if your code exectes inside your if condition in your first page.

<form method="POST" action="Second_page.php">
    <input type="text" name="amountdiv" />
    <input type="text" name="fixedrate" />
    <input type="text" name="deliverydiv" />
    <input type="text" name="date" />
    <input type="text" name="total" />
    <input type="text" name="grandtotal" />
    <input type="text" name="reference" />
    <input type="submit" name="applySubmit" />
</form>

Second_page.php

<?php
   echo "<pre>";
   print_r($_POST);
   echo "</pre>";
?>

<td class="blaah1"><?php echo $_POST['amountdiv'] ;?></td>
<td class="blaah1"><?php echo $_POST['grandtotal'] ;?></td>
<td class="blaah3"><?php echo $_POST['date'] ; ?></td>

You need to assign something to those variables. At the moment, it looks like you're using $amountdiv before you have assigned anything to it, so it will be null .

<?php
session_start();

if (isset($_POST['applySubmit'])) { 
    $amountdiv = $_POST['applySubmit'];
    $_SESSION['amountdiv'] = $amountdiv;

But really, it's all possible in one step:

<?php
session_start();

if (isset($_POST['applySubmit'])) { 
    $_SESSION['amountdiv'] = $_POST['amountdiv'];

Obviously, you should also validate and clean the input. eg

<?php
session_start();

if (isset($_POST['applySubmit'])) { 
    $_SESSION['amountdiv'] = (int)$_POST['amountdiv'];

Or use filter_var() , filter_input() or your own custom filter/clean functions.

Firstpage.php

  <?php
    session_start();

    extract($_POST);

    if (isset($_POST['applySubmit'])) { 
        $_SESSION['amountdiv'] = $amountdiv;
        $_SESSION['fixedrate'] = $fixedrate;
        $_SESSION['deliverydiv'] = $deliverydiv;
        $_SESSION['date'] = $date;  
        $_SESSION['total'] = $total;    
        $_SESSION['grandtotal'] = $grandtotal;      
        $_SESSION['reference'] = $reference;
    }
    ?>
  1. no space before session_start().
  2. and use this 'extract($_POST);' in firstpage.php => its working fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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