rxm
rxm

Reputation: 81

POST data will not enqueue to PHP Circular Queue

I have a Circular Queue I've made using PHP with all of the generic functions you'd see like dequeue, enqueue, peek etc in a file queue.php

I am trying to submit form data using AJAX that has been pre-sanitised to a file save.php with the queue.php included in the file.

queue.php

#prior queue code omitted as it is not relevant to the question
public function enqueue(string $element) { 
      $this->queue[$this->rear] = $element; 
      $this->rear = ($this->rear + 1) % $this->limit; 
  } 
} 

save.php

include("queue.php");
$queue = new CircularQueue(5);

if (isset($_POST['data'])){
    try{
         $queue->enqueue($_POST['data']); 
         echo $queue->peek();
         echo print_r($queue);
    } catch (Exception $e){
         echo $e->getMessage();
    }
}

It successfully manages to enqueue one POST data but any successive AJAX will reset the array and keep storing it as the first index.

e.g. First AJAX data: 20
[0] ==> 20
Second AJAX data: 285
[0] ==> 285

I have checked my queue and it runs as intended when enqueueing in separate lines so the issue is in the save.php file.

Aim: I want any data sent to this save.php file using AJAX to be enqueued accordingly.

e.g. First AJAX data: 20
[0] ==> 20
Second AJAX data: 285
[0] ==> 20 [1] ==> 285

Upvotes: 1

Views: 91

Answers (0)

Related Questions