-1

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?

Community
  • 1
  • 1
Janatan
  • 71
  • 1
  • 3
  • 9
  • Are you POSTing to `First_page.php`? Have you tried `echo`ing something inside your `if` as a basic debugging step to see if they're set? Also, until you have it working, just `var_dump($_SESSION);` at the end of both pages and see what you're getting. – Rudi Visser Mar 15 '13 at 10:17
  • 3
    Think the problem is not with the session, but how you put data in your session. I see you check on a $_POST. So i suggest you want to put something out your form into those session. In that case you should use $_POST['amountdiv'] instant of $amountdiv. Maybe you can post your form ? – S.Visser Mar 15 '13 at 10:18
  • 1
    it is not working because you are not able to fulfull this condition : if (isset($_POST['applySubmit'])) – Dead Man Mar 15 '13 at 10:19
  • Check `Session Save Path`. Make sure it have write permission. – Ashwini Agarwal Mar 15 '13 at 10:20
  • I'm with @S.Visser it's looking like you aren't getting your `POST` values from the form. – George Mar 15 '13 at 10:20
  • @s-visser It's the results from the form what I want, not what has been input into the form. If I do $_POST['amountdiv']. I can probably imagine this will work as its been posted by the user. But 'grandtotal' and 'date' isn't posted by the user. So how would I pass these? – Janatan Mar 15 '13 at 10:30
  • @Janatan Where does that data come from. What you do in your code is checking if there is posted something, if that is the case you put data in your sessions. The problem is that we cant see where this information comes from. – S.Visser Mar 15 '13 at 10:33
  • @S.Visser Here is the form simplified.
    Bill Amount:
    The id 'amount' is where the user inputs the info, the 'amountdiv' is where it is output on the same page. The rest of the form is very similar.
    – Janatan Mar 15 '13 at 10:38
  • The problem is that the data in the div will not be send to the server. A solution for this is to put it in a hidden input field. I will go write an answer for you. – S.Visser Mar 15 '13 at 10:47
  • One more question, how do you put data in the amountdiv? Via javascript ? – S.Visser Mar 15 '13 at 10:52
  • @S.Visser Yes it's via Javascript. – Janatan Mar 15 '13 at 10:53

4 Answers4

0

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.

Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
  • They're being assigned in Javascript. total = amount + delivery; fixedrate = total / 100 * 12.2; grandtotal = fixedrate + total; document.getElementById("amountdiv").innerHTML = amount.toFixed(2); document.getElementById("deliverydiv").innerHTML = delivery.toFixed(2); document.getElementById("total").innerHTML = total.toFixed(2); document.getElementById("fixedrate").innerHTML = fixedrate.toFixed(2); document.getElementById("grandtotal").innerHTML = grandtotal.toFixed(2); – Janatan Mar 15 '13 at 11:39
  • 1
    @Janatan : you need to assign in php before you use it. – Prasanth Bendra Mar 15 '13 at 11:43
0
<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>
Devang Rathod
  • 6,650
  • 2
  • 23
  • 32
0

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. e.g.

<?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.

Daren Chandisingh
  • 2,157
  • 1
  • 13
  • 17
-1

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.
MKV
  • 913
  • 7
  • 6
  • 2
    extract() function is not recommended. As its a security issue – Rohit Choudhary Mar 15 '13 at 10:24
  • Never use extract() on a $_POST. Let say you have an $paymentURL from a payment provider in a config file. Now someone sends a $_POST['paymentURL'] to your file. You extract it, and $paymentURL will be overwriten. – S.Visser Mar 15 '13 at 10:29