使用codeigniter保存数据后显示通知

时间:2016-01-15 02:49:35

标签: javascript php twitter-bootstrap codeigniter

我试图在保存数据时显示通知并在视图中显示,但是不运行我使用notify.js。如果有人能帮助我?

我的控件保存

function save() {
    $data = array(                      
                'idsupplier' => $this->input->post('idsupplier'),
                'namasupplier' => $this->input->post('namasupplier'),
                'alamat' => $this->input->post('alamat'),
                'telp' => $this->input->post('telp'),
                'fax' => $this->input->post('fax'),
                'email' => $this->input->post('email'),
                'web' => $this->input->post('web'),
                'kontak1' => $this->input->post('kontak1'),
                'hp1' => $this->input->post('hp1'),                     
                'email1' => $this->input->post('email1'),
                'kontak2' => $this->input->post('kontak2'),
                'hp2' => $this->input->post('hp2'),                     
                'email2' => $this->input->post('email2'));  
    $this->mcrud->addsupplier($data);   
    echo '<script>Notify("Alat Baru Berhasil Ditambahkan Ke Database ", null, null, type);</script>';

    redirect('instrument/detailsupplier');

我的观点

                <style>
                #notifications {
                    cursor: pointer;
                    position: fixed;
                    right: 0px;
                    z-index: 9999;
                    bottom: 0px;
                    margin-bottom: 22px;
                    margin-right: 15px;
                    max-width: 300px;   
                }
                </style>
     <div id="notifications"></div>

<script>
$( document ).ready(function() {
                Notify("Alat Baru Berhasil Ditambahkan Ke Database ", null, null, type);

    });
</script>

2 个答案:

答案 0 :(得分:2)

使用session flash data

会更容易
function save() {
    $data = array(                      
        'idsupplier' => $this->input->post('idsupplier'),
        'namasupplier' => $this->input->post('namasupplier'),
        'alamat' => $this->input->post('alamat'),
        'telp' => $this->input->post('telp'),
        'fax' => $this->input->post('fax'),
        'email' => $this->input->post('email'),
        'web' => $this->input->post('web'),
        'kontak1' => $this->input->post('kontak1'),
        'hp1' => $this->input->post('hp1'),                     
        'email1' => $this->input->post('email1'),
        'kontak2' => $this->input->post('kontak2'),
        'hp2' => $this->input->post('hp2'),                     
        'email2' => $this->input->post('email2')); 
    );

}

$this->mcrud->addsupplier($data);  

$this->session->set_flashdata('something', 'Alat Baru Berhasil Ditambahkan Ke Database');

// Codeigniter set flash data will only work when your redirecting

redirect('instrument/detailsupplier');

}

然后在你的观点

<?php if ($this->session->flashdata('something')) {?>

    <div id="notifications"><?php echo $this->session->flashdata('something');</div>

<?php }?>

答案 1 :(得分:0)

成功时设置flash消息并检查flash消息并触发Notify。见下面的代码。

function save() {
    $data = array(                      
                'idsupplier' => $this->input->post('idsupplier'),
                'namasupplier' => $this->input->post('namasupplier'),
                'alamat' => $this->input->post('alamat'),
                'telp' => $this->input->post('telp'),
                'fax' => $this->input->post('fax'),
                'email' => $this->input->post('email'),
                'web' => $this->input->post('web'),
                'kontak1' => $this->input->post('kontak1'),
                'hp1' => $this->input->post('hp1'),                     
                'email1' => $this->input->post('email1'),
                'kontak2' => $this->input->post('kontak2'),
                'hp2' => $this->input->post('hp2'),                     
                'email2' => $this->input->post('email2'));  
    $this->mcrud->addsupplier($data);   
    //set flash message
    $this->session->set_flashdata('msg','Alat Baru Berhasil Ditambahkan Ke Database');

    redirect('instrument/detailsupplier');

在您的观看

<?php if ($this->session->flashdata('msg')) {?>
     <script>
      Notify(<?=$this->session->flashdata('msg')?>, null, null, type);
     </script>
<?php } ?>