Codeigniter - Sweetalert没有在控制器中工作

问题描述 投票:0回答:1

当用户在登录时未通过身份验证时,我尝试制作一个sweetalert,但是当页面重新加载时,它不会出现,而不仅仅是sweetalert,带有HTML代码的消息。检查我的控制器代码

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends CI_Controller {

        public function __construct()
        {
            parent::__construct();
            $this->load->model('login_model','', TRUE);
        }

    public function index()
    {       

                if(isset($_POST['password']))
                {
                        $this->load->model('login_model');
                        if($this->login_model->login($_POST['username'],($_POST['password'])))
                        {
                        $this->session->set_userdata('username', $_POST['username']);
                        redirect('home');
                        }else
                        {
                                redirect('login', 'refresh');
                                $var = '<div class="alert alert-success"> <i class="ti-user"></i> This is an example top alert. You can edit what u wish.
                                <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">×</span> </button>
                                </div>';
                                echo $var;
                        }
                }
                $this->load->view('templates/header');
                $this->load->view('login/index');
                $this->load->view('templates/footer');
        }

        public function logout()
        {
            $this->session->sess_destroy();
            redirect('login');
        }

}

我和swal("Hello")一起尝试了这个SWAL类:

$('#sa-basic').each(function(){
    swal("¡Error!", "Datos de autenticación erróneos, vuelva a intentarlo", "error");
});
php jquery codeigniter controller sweetalert
1个回答
0
投票

嘿亚历山大你需要闪存数据以消防swal()。这是我的代码

<?php if ($this->session->flashdata('error')): ?>
<script>
swal({
  title: "Error",
  text: "Your request can not be completed",
  icon: "<?php echo site_url('Your Icon Destination'); ?>",
  button: false,
  timer: 5000,
});
</script>
<?php endif; ?>

这次你可以为成功做出同样的代码

<?php if ($this->session->flashdata('success')): ?>
<script>
swal({
  title: "Congratulation",
  text: "Your request has been completed Successfully!",
  icon: "<?php echo site_url('assets/slim/lib/sweetalert/icon/cancel.svg'); ?>",
  button: false,
  timer: 5000,
});
</script>
<?php endif; ?>

从控制器你可以制作闪存数据

$this->session->set_flashdata('success','Action Completed');

要么

$this->session->set_flashdata('error','Action Not Completed');

如果这有帮助,请投票

© www.soinside.com 2019 - 2024. All rights reserved.