In Codeigniter, if you want to add more functionality or add a beautiful button, then the best option for web developers is – Sweetalert. in this blog, you can understand all about the sweet alert. Let’s start this blog.
What is SweetAlert?
- A BEAUTIFUL, RESPONSIVE, CUSTOMIZABLE, ACCESSIBLE (WAI-ARIA) REPLACEMENT FOR JAVASCRIPT’S POPUP BOXES.
- Sweet Alert has no dependencies. completely free in the browser.
- Look so modern, which means you give a professional feel to your web application.
- Users interact much better when Sweet Alert use in a web application.
- Official Link – Click Here.
How to Add Swal in codeigniter 3?
Before we start to implement Sweet Alert in Codeigniter 3, you must see the below instruction.
- Codeigniter 3 or Above
- A View
- A Controller
- Dependencies of Java script, bootstrap, jquery, ajax or anything you using for making this project.
View File in CodeIgniter 3
In the view file, you just need to add <a> tag with this code or you just simply copy & paste below code to your view file.
My view file name is my_view.php
<a href=”javascript:void(0)” class=”btn btn-danger btn-sm remove”><i class=”fa fa-trash”></i>Click Me </a>
Add Dependencies code below
At the head section
<script src=”https://unpkg.com/sweetalert/dist/sweetalert.min.js”></script>
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js”></script>
<!– Latest compiled and minified CSS –>
<link rel=”stylesheet” href=”https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css” integrity=”sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u” crossorigin=”anonymous”>
Add script code below
Add script below body.
<script>
$(“.remove”).click(function(){
swal({
title: “Are you sure?”,
text: “Once deleted, you will not be able to recover this imaginary file!”,
icon: “warning”,
buttons: true,
dangerMode: true,
})
.then((willDelete) => {
if (willDelete) {
swal(“Poof! Your imaginary file has been deleted!”, {
icon: “success”,
});
} else {
swal(“Your imaginary file is safe!”);
}
})
})
</script>
Controller File in CodeIgniter 3
In the controller file, you must call the view. see below this code.
public function myview()
{
$this->load->view(‘my_view’);
}
Routes File in CodeIgniter 3
Finally, add new routes & call the routes in the browser. Hope everything running well.
$route[‘my-view’] = ‘MyController/myview’;
Download Source Code
Related posts
Thank You
Do connect with us for more CodeIgniter 3 Tips & Tricks. I hope you understand sweet alert. Don’t forget to download Source Code(linked below)