Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6956261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:50:47+00:00 2026-05-27T14:50:47+00:00

I have a Session which I am using to hold items in a form

  • 0

I have a Session which I am using to hold items in a form that are accumulated up by the user until the user wants to proceed to checkout. Its a bit like a Shopping cart where items can be added from the form.

Logical breakdown of code:

  1. Page loads, session starts
  2. If $_SESSION[‘set’] is not set then set it to TRUE.
  3. Display rest of page and form.
  4. User hits “Add another item” button.
  5. Page data gets posted to itself
  6. Page checks that $_SESSION[‘set’] = True and $_POST[‘add_item’] is set.
  7. Page creates a session variables in an array, and adds posted values to those sessions.
  8. Page increments $_SESSION[‘tariff_count’] if more needs to be added

The problem is that my code is not behaving as it should. When I click “Add new tariff” button the first time it does not get caught by my if function. This should be immediately caught. However when I go and press the button again, it finally works and adds an item to my session.

Here is the code:

//start a session to remember tariff items 
session_start();

//testing the session array
print_r($_SESSION);

//destroy session if this character is found in URL string
$des = $_GET['d'];
if($des == 1)
{
  session_destroy();
}

//checks to see if session data has been set

//if a session variable count is set then 
if ($_SESSION['set'] == TRUE)
{
  //perform a check to ensure the page has been called by the form button and not been accidently refreshed
  if(isset($_POST['add_tariff']))
  { 
    //if user clicks Add another tariff button then increase tariff count by one

    //temp variable set to the current count of items added
    $count = $_SESSION['tariff_count'];
    $_SESSION['tariff_name'][$count] = $_POST['tariff_name'];
    $_SESSION['tariff_net'][$count] = $_POST['tariff_net'];
    $_SESSION['tariff_inclusive'][$count] = $_POST['tariff_inclusive'];
    $_SESSION['tariff_length'][$count] = $_POST['tariff_length'];
    $_SESSION['tariff_data'][$count] = $_POST['tariff_data'];
    //increment tariff count if more data needs to be added to the sessions later.
    $_SESSION['tariff_count']++;
  }
}
//if no session data set then start new session data
else
{   
  echo "session set";
  $_SESSION['set'] = TRUE;
  $_SESSION['tariff_count'] = 0;
}

The code seems to be fudging my arrays of Sesssion data. All my added items in the session are displayed in a table.

However if my table shows six items, if i do a print_r of the session it only shows there are 4 items in the array? I have tested it to make sure I am not reprinting the same instances in the array.

Here is a print_r of the array that shows six rows but there are only four rows in this array?

[tariff_count] => 5 [tariff_name] => Array (
    [0] => STREAM1TARIFF [1] => STREAM1TARIFF [2] => CSS [3] => CSS [4] => CSS
  ) 

I have take a screenshot as well to show this strange problem

https://i.stack.imgur.com/ARv1F.png

Note I have echoed out “True Value =6” but in the print_r of the session it is only 5, so my code is missing out one instance (n-1).

Here is my code that prints all the instances in the session arrays, I have a feeling part of the problem in mismatch is caused by the “<=” comparison?

if(isset($_SESSION['tariff_count']))
{
  for ($i = 0; $i <= $count; $i++)
  {
    echo "<tr>";
    echo "<td>".$_SESSION['tariff_name'][$i]."</td>";
    echo "<td>".$_SESSION['tariff_net'][$i]."</td>";
    echo "<td>".$_SESSION['tariff_inclusive'][$i]."</td>";
    echo "<td>".$_SESSION['tariff_length'][$i]."</td>";
    echo "<td>".$_SESSION['tariff_data'][$i]."</td>";
    echo "</tr>";
  }
}

Paste bin of php page – http://pastebin.com/petkrEck

Any ideas, why my If statement is not catching the event when the user presses “Add another tariff” button the first time it is pressed, but then detects it afterwards?

Thanks for your time

Merry Christmas!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:50:48+00:00Added an answer on May 27, 2026 at 2:50 pm

    The problem is your code flow. In simplified pseudo-code, you’re doing this:

    if (session is not initialized) {
        set = true
        count = 0;
    } else {
        add posted data to session
    }
    

    On the first ‘add item’ call, the session is not set up, so you set up the session. AND THEN IGNORE THE POSTED DATA.

    The code flow should be:

    if (session is not initialized) {
        set = true;
        count = 0;
    }
    
    if (posting data) {
       add data to session
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using .net 1.1. I have a session class in which I have
I have a form which collects user info including name, location, url, email address
I have an application which works on a user's session which is maintained between
I currently have a custom session handler class which simply builds on php's session
I have a table (session) in a database which has almost 72,000 rows. I
I have a list of objects which I store in the session. This list
I have read the page in Emacs wiki which contains a list of session
I almost always have a Scala REPL session or two open, which makes it
I have a session class that needs to store session information in a MySQL
I have a class called Session which exposes several public methods. I'd like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.