0

I am working on a proces where a customer can fill in his dimensions and then can submit a form which data then will be saved in SESSION variables and will be displayed on the page, so the customer can watch his configuration.

I save my submit in $_SESSION['wz_submit_saving_1'] and use an If statement to check if the form is submitted so the config can be displayed.

<?php // Save submit
$wz_submit_saving_1 = $_POST['wz_submit_saving_1'];
$_SESSION['wz_submit_saving_1'] = $wz_submit_saving_1;

if(isset($_SESSION['wz_submit_saving_1'])) :

    // Save wz_saving_a in session
    $wz_saving_a = $_POST['wz_saving_a'];
    $_SESSION['wz_saving_a'] = $wz_saving_a;

    // Save wz_saving_b in session
    $wz_saving_b = $_POST['wz_saving_b'];
    $_SESSION['wz_saving_b'] = $wz_saving_b;

endif; ?>

My problem is that if I use if(isset($_SESSION['wz_submit_saving_1'])) for showing the config, it displays well after submit, but if I refresh the page the config is gone.

If I use if(isset($_SESSION['wz_saving_a'])) (the a dimension field) and refresh the page, the config is almost there. But I want to use the saved submit session variable for checking if the form is submitted. Can someone tell me what I am doing wrong?

<?php if(isset($_SESSION['wz_submit_saving_1'])) : ?>

<div id="wz_config_1" class="wz_config">

   <ul>
      <li>Dimensions</li>
      <li>A: <?php if(isset($_SESSION['wz_saving_a'])) : echo $_SESSION['wz_saving_a']; endif; ?></li>
      <li>B: <?php if(isset($_SESSION['wz_saving_b'])) : echo $_SESSION['wz_saving_b']; endif; ?></li>
   </ul>

</div><!--End wz_config_1-->

<?php endif; ?>   

The form:

<form method="POST">

   <label>A</label>
   <input name="wz_saving_a" type="text" />  

   <label>B</label>
   <input name="wz_saving_b" type="text" />

   <input name="wz_submit_saving_1" type="submit" class="add_button" value="Add"  />

</form>   
Robbert
  • 1,270
  • 6
  • 30
  • 62

2 Answers2

1

Are you refreshing the same page without doing a POST?

It may be due to the code here:

$wz_submit_saving_1 = $_POST['wz_submit_saving_1'];
$_SESSION['wz_submit_saving_1'] = $wz_submit_saving_1;

Because you're just doing a GET on refresh, the session variable wz_submit_saving_1 is being overwritten by an empty wz_submit_saving_1 POST variable.

EDIT:

<?php // Save submit
if(isset($_POST['wz_submit_saving_1'])) :
    $_SESSION['wz_submit_saving_1'] = $_POST['wz_submit_saving_1'];

    // Save wz_saving_a in session
    $wz_saving_a = $_POST['wz_saving_a'];
    $_SESSION['wz_saving_a'] = $wz_saving_a;

    // Save wz_saving_b in session
    $wz_saving_b = $_POST['wz_saving_b'];
    $_SESSION['wz_saving_b'] = $wz_saving_b;

endif; ?>
russellc
  • 494
  • 3
  • 7
  • Yes I refresh without doing a POST. So you say that I can't check it with the submit session variable? – Robbert Nov 21 '13 at 10:42
  • If you are refreshing to the same page, then the `wz_submit_saving_1` variable will always end up with an empty value (because you are not providing any POST variables on refresh, only GET). – russellc Nov 21 '13 at 10:47
  • Ok, I understand. So what is your advice? Using the other variables for check? – Robbert Nov 21 '13 at 10:51
  • There are a few amendments I would make. It would be necessary to check if you are receiving a POST request before modifying the `wz_submit_saving_1` session variable. Secondly, the same problem will arise inside your current conditional. I would save that data on POST request only. I'll amend my original post with some code. – russellc Nov 21 '13 at 10:57
  • I've amended my original post with some code. Let me know if that works out for you! – russellc Nov 21 '13 at 11:00
  • Great! You're welcome. EDIT: just did a quick amendment on my original post, had an unnecessary conditional up top – russellc Nov 21 '13 at 11:07
0
<?php // Save submit
if(isset($_POST['wz_submit_saving_1'])){
    $_SESSION['wz_submit_saving_1'] = $_POST['wz_submit_saving_1'];
}
if(isset($_SESSION['wz_submit_saving_1'])) :

    // Save wz_saving_a in session
    $wz_saving_a = $_POST['wz_saving_a'];
    $_SESSION['wz_saving_a'] = $wz_saving_a;

    // Save wz_saving_b in session
    $wz_saving_b = $_POST['wz_saving_b'];
    $_SESSION['wz_saving_b'] = $wz_saving_b;

endif; ?>
Vipin Kumar Soni
  • 826
  • 10
  • 18