2

I'm creating an ticket booking application. I'm trying to create a basic cart using PHP and Ajax, When i click on the add to cart button, It sends the seat number to the "seatchecker.php" file using Ajax which checks if the seat is available, Then if it's available, It sends that seat number to the "seatadder.php" file using Ajax which should add the seat number to the Session array. But each time i click "Add to cart" it just displays the new value, Rather than showing the whole cart. May be it's overwriting the session variable each time? any help would be appreciated. Thanks

<?php
session_start();
// Getting the value sent by checkseats.php using ajax
$seat_added = $_GET['seatadd'];
// ARRAY OF SESSION VARIABLE
$_SESSION['seat_add'] = array();
function multiple_seats_adder($getseat){
  array_push($_SESSION['seat_add'],$getseat);
  // TESTING
  print_r($_SESSION['seat_add']);
  // TESTING
  echo sizeof($_SESSION['seat_add']);
}
echo multiple_seats_adder($seat_added);
?>

1 Answer 1

1

The Problem seems to stem from the Fact that you are initializing the seat_add Key to an Empty Array each time the Script is called. Most likely, that is not what you want. Consider the Code below:

    <?php
        session_start();
        // Getting the value sent by checkseats.php using ajax
        $seat_added = $_GET['seatadd'];

        // ONLY INITIALIZE THIS TO AN EMPTY ARRAY IF IT DOESN'T EXIST AT ALL:
        if(!isset($_SESSION['seat_add'])){
            // ARRAY OF SESSION VARIABLE
            $_SESSION['seat_add'] = array();                
        }


        function multiple_seats_adder($getseat){
            array_push($_SESSION['seat_add'], $getseat);
            // TESTING
            print_r($_SESSION['seat_add']);
            // TESTING
            echo sizeof($_SESSION['seat_add']);
        }
        multiple_seats_adder($seat_added);
Sign up to request clarification or add additional context in comments.

1 Comment

@Khuzema If it solved your Problem, you could check it as the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.