【问题标题】:How to submit multiple requests to the function to send all submitted requests如何向函数提交多个请求以发送所有提交的请求
【发布时间】:2021-09-16 10:24:06
【问题描述】:

我正在尝试向一个函数提交几个请求,该函数将它们写入一个会话,然后返回它们

这里我将数据提交给函数

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

这是我们使用的函数

    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;
        
        
    }

通话会话

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

当它执行脚本时,它只返回最后一个请求 - $this-&gt;Notification-&gt;build('error', 'Test1', 'danger', 'bottom-left', 0, 'true');

【问题讨论】:

  • 你不只是重写你的会话notify键吗?最后写入获胜...
  • 如何让它保存许多查询

标签: php session


【解决方案1】:

您可以传递一个数组,否则您所做的就是每次都覆盖会话键值,所以稍微修改一下方法并将数组传递给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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-21
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多