Skip to content
Advertisement

How to submit multiple requests to the function to send all submitted requests

I’m trying to submit several requests to a function that writes them to a session that then returns them

here I submit the data to the function

$this->Notification->build('error', 'Test', 'danger', 'bottom-left', 0, 'true');
$this->Notification->build('error', 'Test1', 'danger', 'bottom-left', 0, 'true');

this is the function we use

    public function build($title, $description,  $type, $position, $closeTimeout, $showProgress)
    {

        $notify     = '<script>';
        $notify    .= 'Notification.notify({';
        $notify    .= 'title: "'.esc($title).'",';
        $notify    .= 'description: "'.esc($description).'",';
        $notify    .= 'image: {visible: true},';
        $notify    .= 'type: "'.$type.'",';
        $notify    .= 'position: "'.$position.'",';
        $notify    .= 'closeTimeout: "'.$closeTimeout.'",';
        $notify    .= 'showProgress: "'.$showProgress.'",';
        //$notify.    .= '';
        //$notify.    .= '';
        //$notify.    .= '';
        //$notify.    .= '';
        $notify    .= '});</script>';

        $notify = $this->Session->setFlashData("notify", $notify); // records a session named "notify"
        

        return  true;
        
        
    }

call session

      <?php if (session()->get('notify')) : ?>
      <?= session()->get('notify'); ?>
      <?php endif; ?>

when it executes the script it returns the last request only – $this->Notification->build('error', 'Test1', 'danger', 'bottom-left', 0, 'true');

Advertisement

Answer

You can pass an array but otherwise what you are doing is overwriting the session keys value each time, so modofy the methid a little and pass an array to the setFlashData()

public function build($title, $description,  $type, $position, 
                      $closeTimeout, $showProgress)
{

    $notify     = '<script>';
        $notify    .= 'Notification.notify({';
            $notify    .= 'title: "'.esc($title).'",';
            $notify    .= 'description: "'.esc($description).'",';
            $notify    .= 'image: {visible: true},';
            $notify    .= 'type: "'.$type.'",';
            $notify    .= 'position: "'.$position.'",';
            $notify    .= 'closeTimeout: "'.$closeTimeout.'",';
            $notify    .= 'showProgress: "'.$showProgress.'",';
        $notify    .= '});</script>';
    return  $notify;
}


// call it
$flash = [];
$flash[] = $this->Notification->build('error', 'Test', 'danger', 'bottom-left', 0, 'true');
$flash[] = $this->Notification->build('error', 'Test1', 'danger', 'bottom-left', 0, 'true');

$this->Session->setFlashData("notify", $flash);
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement