Showing posts with label checkbox. Show all posts
Showing posts with label checkbox. Show all posts

Tuesday, 14 April 2020

Getting multiple checkboxes/checkbox group values in JSP.


In this post, we look that how can get the multiple checkbox/checkbox group values using JSP.

So first craete multiple checkboxes:-

<form id="" action="get_choice.jsp" method="post">

<input name='cid' type='checkbox' value='value1' />
<input name='cid' type='checkbox' value='value2' />
<input name='cid' type='checkbox' value='value3' />
<input name='cid' type='checkbox' value='value4' />

</form>

Now where we want to recieve these values (in my case it is get_choices.jsp)
In String array get these values-

String[] cid=request.getParameterValues("cid");

if(cid!=null)
{
for(int i=0;i<cid.length;i++)
{
out.print(cid[i]);
}
}

You can also store these values in different variables for further processing like-

String[] cid; String ch1,ch2,ch3;

cid=request.getParameterValues("cid");

if (cid != null)
{
for(int i=0;i<cid.length;i++)
{
if(i==0)
ch1=cid[0];
if(i==1)
ch2=cid[1];
if(i==2)
ch3=cid[2];

}
}




Saturday, 3 September 2016

Multi select Checkbox

We stuck at some situation where checkboxes are generated dynamically- generally when we fetch data from database- and we have to select  some row are all rows and do some operation on that.

If we don't have multi select checkbox option we will have to select one by one and if there are too many data it will be time-consuming and irritating.

To do this we require Jquery with javascript.

1. Add jquery.min.js to file-

<script src="js/jquery.min.js"></script>

2. Use javascript to file-

$(document).ready(function(){
    $('#select_all').on('click',function(){
        if(this.checked){
            $('.checkbox').each(function(){
                this.checked = true;
            });
        }else{
             $('.checkbox').each(function(){
                this.checked = false;
            });
        }
    });
    
    $('.checkbox').on('click',function(){
        if($('.checkbox:checked').length == $('.checkbox').length){
            $('#select_all').prop('checked',true);
        }else{
            $('#select_all').prop('checked',false);
        }
    });
});

3. Then create master checkbox-

<input type='checkbox' id='select_all' />

4. Now another checkboxes will be-


<input name='rid[]' type='checkbox' class='checkbox' value='<?php echo $row[0];?>' />

All done .

It looks like-


 




Change image source dynamically on hyperlink

 Changing image source dynamically using JQuery. Here in this example I have created there hyperlink and stored all images in the same folde...