Codeigniter-Sweetalert在控制器中无效

时间:2019-02-05 00:24:21

标签: php jquery codeigniter controller 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");
});

1 个答案:

答案 0 :(得分: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; ?>

您可以通过控制器制作类似Flash数据

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

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

如果有帮助,请投票