How to retain Hidden Fields With Sessions?

I’m working with a tabbed menu system. For example, I have four tabs. Each tab is a separate ‘page’ with forms (it’s all on one web page, but separated in sections).

The first tab has a form with a radio button selection. I thought that in order retain that user’s selection, I would need to create a hidden field and then create sessions so that info. will forward that information to the final form (#4) to list the information.

But when I put this in my header:

<?php
session_start();

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

?>

I get this error:
Notice: Undefined index: WebPkg in C:\blahblah\website-plans.php on line 4

At the bottom of my code I have two hidden fields to ‘post’ the information. Where am I going wrong?

<form id="form1" name="form1" method="post" action="">
<table width="875" border="0" cellpadding="1" cellspacing="1" class="select-plan-table">
<tr class="select-plan">
    <td class="select-plan"><div class="pagenumber">1</div>
      <h2>Page <br />
        Website</h2>
        <p>Custom Web Design<br />
        Free Hosting ($14.99/mo value)<br />
        Rewebbed CMS <img src="../images/tooltips.png" alt="More Info" width="13" height="13" /><br />
        Google Analytics <img src="../images/tooltips.png" alt="More Info" width="13" height="13" /><br />
        On-Site Optimization        <img src="../images/tooltips.png" alt="More Info" width="13" height="13" /></p>   </td>
    <td class="select-plan"><div id="most-popular"></div><div class="pagenumber">5</div>
      <h2>Page <br />
        Website</h2>
        <p>Custom Web Design<br />
        Free Hosting ($14.99/mo value)<br />
        Rewebbed CMS <img src="../images/tooltips.png" alt="More Info" width="13" height="13" /><br />
        Google Analytics <img src="../images/tooltips.png" alt="More Info" width="13" height="13" /><br />
    On-Site Optimization        <img src="../images/tooltips.png" alt="More Info" width="13" height="13" /></p></td>
    <td class="select-plan"><div class="pagenumber">10</div>
      <h2>Page <br />
        Website</h2>
        <p>Custom Web Design<br />
        Free Hosting ($14.99/mo value)<br />
        Rewebbed CMS <img src="../images/tooltips.png" alt="More Info" width="13" height="13" /><br />
        Google Analytics <img src="../images/tooltips.png" alt="More Info" width="13" height="13" /><br />
    On-Site Optimization        <img src="../images/tooltips.png" alt="More Info" width="13" height="13" /></p></td>
    <td class="select-plan"><div class="pagenumber">15</div>
      <h2>Page <br />
        Website</h2>
        <p>Custom Web Design<br />
        Free Hosting ($14.99/mo value)<br />
        Rewebbed CMS <img src="../images/tooltips.png" alt="More Info" width="13" height="13" /><br />
        Google Analytics {yootooltip title=[<img src="../images/tooltips.png"  />]}Tooltip Content{/yootooltip} <br />
    On-Site Optimization        <img src="../images/tooltips.png" alt="More Info" width="13" height="13" /></p></td>
  </tr>
  <tr>
    <td class="plans-price"> <span class="price-big"><input value="39.99" title="" class="radio" id="WebsitePkg1" name="WebPkg" type="radio" />$39</span><span class="small-price">.99/mo</span></td>
    <td class="plans-price"><span class="price-big"><input value="79.99" title="" class="radio" id="WebsitePkg5" name="WebPkg" type="radio" />$79</span><span class="small-price">.99/mo</span></td>
    <td class="plans-price"><span class="price-big"><input value="114.99" title="" class="radio" id="WebsitePkg10" name="WebPkg" type="radio" />$114</span><span class="small-price">.99/mo</span></td>
    <td class="plans-price"><span class="price-big"><input value="149.99" title="" class="radio" id="WebsitePkg15" name="WebPkg" type="radio" />$149</span><span class="small-price">.99/mo</span></td>
  </tr>
</table>
<p><input class="cf_inputbox" maxlength="150" size="30" title="" id="addpages" name="addpages" type="text" value="0"/>
     Add Additional Pages To Any Plan <br />
      <span class="style1">$9.99/mo each</span></p>
</div>
<h4>Need Help Creating A Plan?</h4>
<p><button style="background: none; border: none; width: 135px; height: 33px; border: none; float: right; cursor: pointer;" onclick="TabbedPanels1.showPanel(1); return false;" ><img alt="Next" src="../images/stories/add-ons.png" height="33" width="135" /></button>
        <script type="text/javascript">
            var TabbedPanels1 = new Spry.Widget.TabbedPanels("TabbedPanels1");
        </script>
        Our <strong>Site Map Worksheet</strong> will help you strategize your website. <a target="_blank" href="#">Download PDF</a><br /><a href="#">View Example Site Map</a>
<input name="WebPkg" type="hidden" id="WebPkg" value="<?php echo((isset($_POST["WebPkg"]))?$_POST["WebPkg"]:"") ?>" />
<input name="addpages" type="hidden" id="addpages" value="<?php echo((isset($_POST["addpages"]))?$_POST["addpages"]:"") ?>" />
</p>
</form>

The error means that the array index in $_POST doesn’t exist so rather then the script just assuming its there you need to use the isset() function like you did at the end of your script.

$_SESSION['WebPkg'] = isset($_POST['WebPkg']) ? $_POST['WebPkg'] ; '';

The first time you load the page, there is no $_POST - you havent submitted the form data yet.

try doing an if-isset around that line.


if(isset($_POST['WebPkg'])) {
$_SESSION['WebPkg'] = $_POST['WebPkg']; 
}

EDIT: SL strikes again!

Off Topic:

hehe I’m a ninja

Great ideas, guys and I’ll try to implement those as my project moves forward.

How would this work with checkbox variables?

It depends whether your checking for a single checkbox or an array of checkboxes, for a single checkbox the above will work fine but for an array you would need to use something like the below.

if (isset($_POST['checkbox_array']) && sizeof($_POST['checkbox_array']) > 0) {
    // Do something with the array of checkboxes
} else {
    // Nothing has been posted
}