-1

i have a page in php which loads a group of category from another php script and those are check boxes.

format of check box

<input type='checkbox' class='cat-selector' id='Bussiness' data-toggle='checkbox'/>
<input type='checkbox' class='cat-selector' id='Marketting' data-toggle='checkbox'/> 

Now if I have url localhost/newproj/search?category=Business i want to check those category check boxes which are in the url

chaitanyasingu
  • 121
  • 1
  • 13

4 Answers4

1
<input type='checkbox' class='cat-selector' id='Bussiness' data-toggle='checkbox' <?php echo (isset($_GET['Category']) && $_GET['Category'] == 'Bussiness')?'checked':''?>/>
<input type='checkbox' class='cat-selector' id='Marketting' data-toggle='checkbox' <?php echo (isset($_GET['Category']) && $_GET['Category'] == 'Marketting')?'checked':''?>/>
vaibhavmande
  • 1,115
  • 9
  • 17
0

you want to do something like this in jquery/client side code to see if your checkbox id is the same as the category parameter.

$.ajax({
    url : '/newproj/search?category=Business',
    success : function(data){
       $(".cat-selector").each(function(){
       if($(this).prop("id") === getParameterByName("category")){
          $(this).prop("checked","checked");
       }
    }); 
    } });

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

you would need jQuery 1.8 or later to use the prop function. Else you would have to use the attr function.

you could also do it on the server side as suggested in other answers.

Arvind Sridharan
  • 3,885
  • 4
  • 29
  • 54
0

Maybe you can use window.location.pathname

See this question: Get current URL in JavaScript?

And then combine with jquery with attr('checked','checked') http://api.jquery.com/attr/

Community
  • 1
  • 1
dcarreroc
  • 49
  • 1
0

Try this:

<input type='checkbox' class='cat-selector' id='Bussiness' value='Business' data-toggle='checkbox' <?php
echo (isset($_REQUEST['category']) && $_REQUEST['category']=='Business')?'checked':'' ?> />
Business
<input type='checkbox' value='Marketing' class='cat-selector' id='Marketting' data-toggle='checkbox' <?php
echo (isset($_REQUEST['category']) && $_REQUEST['category']=='Marketting')?'checked':'' ?> />
Marketing
Anand Solanki
  • 3,419
  • 4
  • 16
  • 27