Multiselect dropdown with checkboxes demo code in php

Being a developer you may face a situation, have to give multiple select option for the front end user, now it is easy with the help of bootstrap multiselect script we can accomplish that task within 10 minutes. Here am going to explain you Multi select drop down with check boxes demo code in php

here is the Multiselect dropdown with checkboxes demo code in php

Step 1

Files To Include

<script type="text/javascript" src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css">
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/css/bootstrap-multiselect.css">
<script type="text/javascript" src="//cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/js/bootstrap-multiselect.js"></script>

Step 2

HTML Form

<form id="myForm" method="POST">
<input name="current_select_values" type="hidden" value="" id="current_select_values">
<div class="col-md-12" >
 <select size="5" name="current_select" multiple="multiple" id="current_select">
 <option value="current1">current1</option>
 <option value="current2">current2</option>
 <option value="current3">current3</option>
 <option value="current4">current4</option>
 <option value="current5">current5</option>
 </select>
 </div><div class="col-md-12" style="margin-top:20px;" >
 <input type="submit" value="Submit" ></div>
</form>

Step 3

Script Files

<script>
 $("#myForm").on("change", "input:checkbox", function(){
 $("#myForm").submit();
 });
var data = "<?php echo $slected_current_data; ?>";
var dataarray=data.split(",");
$("#current_select").val(dataarray);
$(function () {
 $('#current_select').multiselect({ 
 buttonText: function(options, select) {
 var labels = [];
 console.log(options);
 options.each(function() {
 labels.push($(this).val());
 });
 $("#current_select_values").val(labels.join(',') + '');
 return labels.join(', ') + '';
 //}
 }

 });
});

</script>

Step 4

Post Action

<?php 
$slected_current_data='';
 if(isset($_POST['current_select_values']))
 {
 $slected_current_data=$_POST['current_select_values']; 
 }
 else
 {
 $slected_current_data="current1"; 
 }

?>

See Demo Code

Multiselect dropdown with checkboxes demo code in php

php code to check if function exists or not

when we are doing coding a project,their is a chance of misspelling the function name it will end up with an error function does not exists error php,so before calling a function you have to check if the function is already define or not,for we have simple php code to check if function exists or not here is the syntax

function_exists(FUNCTION_NAME_HERE);

This will Return true if the function is defined

e.g

if(function_exists('new_func')){
  //new_func is defined

  new_func();  call that function  
}

This function we can use at the time of defining a function,otherwise you will get an error message saying already the function “new_func” is defined

So for before all fucntion declare case you have to use function_exists(‘new_func’) fucntion to double confirm no where the function is define more than one times

see the error message here

php-code-to-check-if-function-exists-or-not

if(!function_exists('new_func')){
 function new_func()
 {
  echo "php code to check if function exists or not";	 
 }
}

Out put
php code to check if function exists or not

Multi Item Carousel slide sample code

Here am going to show you how to add Multi Item Carousel slide in your website here is the Multi Item Carousel slide sample code

1) Script files to include in the header

<link rel="stylesheet" href="https://owlcarousel2.github.io/OwlCarousel2/assets/owlcarousel/assets/owl.carousel.min.css">
<link rel="stylesheet" href="https://owlcarousel2.github.io/OwlCarousel2/assets/owlcarousel/assets/owl.theme.default.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script src="https://owlcarousel2.github.io/OwlCarousel2/assets/owlcarousel/owl.carousel.js" type="90e6e4b8e06878c6e6ff42b7-text/javascript"></script>

2)  CSS Part


<style type="text/css">
 .carousel-wrap {
 width: 1000px;
 margin: auto;
 position: relative;
 }
 .owl-carousel .owl-nav{
 overflow: hidden;
 height: 0px;
 }

 .owl-theme .owl-dots .owl-dot.active span, 
 .owl-theme .owl-dots .owl-dot:hover span {
 background: #2caae1;
 }


 .owl-carousel .item {
 text-align: center;
 }
 .owl-carousel .nav-btn{
 height: 47px;
 position: absolute;
 width: 26px;
 cursor: pointer;
 top: 100px !important;
 }

 .owl-carousel .owl-prev.disabled,
 .owl-carousel .owl-next.disabled{
 pointer-events: none;
 opacity: 0.2;
 }

 .owl-carousel .prev-slide{
 background: url(img/icon.png) no-repeat scroll 0 0;
 left: -33px;
 }
 .owl-carousel .next-slide{
 background: url(img/icon.png) no-repeat scroll -24px 0px;
 right: -33px;
 }
 .owl-carousel .prev-slide:hover{
 background-position: 0px -53px;
 }
 .owl-carousel .next-slide:hover{
 background-position: -24px -53px;
 }

 span.img-text {
 text-decoration: none;
 outline: none;
 transition: all 0.4s ease;
 -webkit-transition: all 0.4s ease;
 -moz-transition: all 0.4s ease;
 -o-transition: all 0.4s ease;
 cursor: pointer;
 width: 100%;
 font-size: 23px;
 display: block;
 text-transform: capitalize;
 }
 span.img-text:hover {
 color: #2caae1;
 }
</style>

 

3) JQuery script to initialize

 

 <script type="text/javascript">
 $(document).ready(function() {

 $('.owl-carousel').owlCarousel({
 loop: true,
 dots: false,
 margin: 10,
 nav: true,
 navText:["<div class='nav-btn prev-slide'></div><div class='nav-btn next-slide'></div>"],
 responsive: {
 0: {
 items: 1
 },
 600: {
 items: 3
 },
 1000: {
 items: 3
 }
 }
 });
 });
 </script>

4) Finally HTML

 

<div class="carousel-wrap" style="text-align: center;">
<div class="owl-carousel owl-theme">
<div class="item">

<img src="/img/tutorialshore.jpeg" /><span class="img-text">tutorialshore</span>

</div>
<div class="item">

<img src="/img/tutorialshore.jpeg" /><span class="img-text">tutorialshore</span>

</div>
<div class="item">

<img src="/img/tutorialshore.jpeg" /><span class="img-text">tutorialshore</span>

</div>
<div class="item">

<img src="/img/tutorialshore.jpeg" /><span class="img-text">tutorialshore</span>

</div>
<div class="item">

<img src="/img/tutorialshore.jpeg" /><span class="img-text">tutorialshore</span>

</div>
<div class="item">

<img src="/img/tutorialshore.jpeg" /><span class="img-text">tutorialshore</span>

</div>
<div class="item">

<img src="/img/tutorialshore.jpeg" /><span class="img-text">tutorialshore</span>

</div>
<div class="item">

<img src="/img/tutorialshore.jpeg" /><span class="img-text">tutorialshore</span>

</div>
<div class="item"><img src="/img/tutorialshore.jpeg" />
<span class="img-text">tutorialshore</span></div>
<div class="item"><img src="/img/tutorialshore.jpeg" />
<span class="img-text">tutorialshore</span></div>
<div class="item">

<img src="/img/tutorialshore.jpeg" /><span class="img-text">tutorialshore</span>

</div>
<div class="item">

<img src="/img/tutorialshore.jpeg" /><span class="img-text">tutorialshore</span>

</div>
<div class="item">

<img src="/img/tutorialshore.jpeg" /><span class="img-text">tutorialshore</span>

</div>
</div>
<script src="https://ajax.cloudflare.com/cdn-cgi/scripts/2448a7bd/cloudflare-static/rocket-loader.min.js" ></script>

</div>

See Demo>

How to Set a cookie attribute Samesite value in PHP ?

What is samesite cookie in php

We all know most of the website using cookie for sharing information between browsers and the server,so cookie is storing in the local browsers,so their is a probability of misused by other domain which we are using in out html code.for example when am adding Instagram images in my blog tutorialshore.com,i have to use Instagram domain name in my html code to point Instagram images, the cookies in the browser that belong to these other sites will also be sent.Therefore those third parties Instagram can track your activity by using Cookie. Fortunately we have cookie attribute called samesite,by setting a cookie to samesite strict we can prevent third party misuse of cookies.samesite cookie attribute having two values Strict and Lax.AS the name says Strict completely prevent the cookie will not be sent along with requests initiated by third party websites.But in the case of Lax only get method cookie will be sent along with requests initiated by third party websites

Method

 we have a method to accomplish this,which can be done by one of the most common function header() in php


Add cookie Samesite strict by using header() function 

add below header() functions in the top of your home page

header("Set-Cookie: key=value; path=/; domain=www.tutorialshore.com; HttpOnly; Secure; SameSite=Strict");

See how we can check a cookie is samesite, we can do with the help of chrome browser as we know almost every browser have the inspect element option, within inspect element tab we have the application section to check cookie see below image.
Click F12 function button go for application => cookies

set a cookie samesite value in PHP