【问题标题】:send variable to $this->session->set_flashdata将变量发送到 $this->session->set_flashdata
【发布时间】:2019-05-17 11:14:14
【问题描述】:

我正在尝试上传图片,并想在以下代码中获取 set_flashdata 中的错误,无法将错误消息作为变量发送。

public function update_data($id)
{

$config['upload_path']   = "./school_folder/users_picture"; 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['max_size']      = '2048'; 
     $config['max_width']     = '1024'; 
     $config['max_height']    = '768'; 
     $config['file_name'] = $id;
     $config['encrypt_name'] = false; 
     $this->load->library('upload', $config);
     $this->upload->overwrite = true;

            if ( ! $this->upload->do_upload('image'))
            {       // in case of fuilure
// the following portion of code don't show the error message 

                    $error =  $this->upload->display_errors();
                    $this->session->set_flashdata('item',$error );
redirect('index.php/Users/students_edit/'.$id);
                            }
            else // in case of success 
            {
                    $this->session->set_flashdata('item', 'Successfully uploaded' );
redirect('index.php/Users/students_edit/'.$id);


                }              

这里是 $error 的调试:

$error =  $this->upload->display_errors();
echo $error;
die();

它显示以下内容:

您尝试上传的图片不符合允许的范围 尺寸。

【问题讨论】:

  • 那么你的问题是什么?
  • 我想在重定向到视图时显示错误消息。
  • 那么您是否检查了 $error 包含的内容,通过制作调试输出并注释掉重定向...?
  • 是的,它有这个错误:您尝试上传的图片不符合允许的尺寸。
  • 请编辑您的问题并添加用于显示错误的代码,因为这就是您要询问的内容。

标签: php codeigniter session


【解决方案1】:
$this->session->set_flashdata('item', 'Successfully uploaded');

view.php

<?php if ($this->session->flashdata('msg')) { ?>
    <div id="mydiv" class="alert alert-success"><?= $this->session->flashdata('category_success') ?>
    <?php
        echo $this->session->flashdata('msg');
    ?>
    </div>
<?php } ?>

查看此链接

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

【讨论】:

  • $this->session->set_flashdata('item','There is somting wrong.' );重定向('index.php/Users/students_edit/'.$id);这将显示消息。
  • $error = $this->upload->display_errors(); $this->session->set_flashdata('item',$error );重定向('index.php/Users/students_edit/'.$id);
  • 但这并没有显示变量 $error 中的内容
  • set_flashdata('item',$error) 我没有使用 2 个不同的,可能是拼写错误。
  • session->flashdata('item')) { ?>
    = $ this->session->flashdata('category_success') ?> session->flashdata('item'); ?>
猜你喜欢
  • 2012-10-18
  • 2012-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-21
  • 2011-10-17
  • 2015-03-10
相关资源
最近更新 更多