Jump to content

Filter with Multiple Checkboxes


hal_11

Recommended Posts

Hi all, iv been trying to compile a script that will allow users to select multiple checkboxs that will filter results from db..but to no avail.  I have my data stored in mysql and have the queries saved seperatley. i.e. price = 0-500, 500-1000.etc.

 

What im trying to achieve is to allow users to select a checkbox that will execute that particular query and also more importantly allow them to select 'multiple' checkboxes that will filter the results accordingly.  I have never attempted to code this before although I am aware that it might involve arrays.  If someone could give me a nudge in the right direction that would be great :-)

Link to comment
Share on other sites

Html:

<input type='checkbox' name='money[]' value='1' />

<input type='checkbox' name='money[]' value='2' />

<input type='checkbox' name='money[]' value='3' />

 

Php:

$type = $_GET['money'];

if($type){

if(is_array($type)){

foreach($type as $x){

  echo "$x\n";

}

}

}

 

You can look at the global by

 

print_r($_GET);

Link to comment
Share on other sites

Thanks for the reply, Im not really sure how this will retrieve the information  from the database through each query though  :shrug:..im kind of a  newb at this area of coding!...Iv provided a link to what im trying to achieve, that will explain it better than i ever could.  http://www.dfs.co.uk/sofas/leather-sofas/

 

The dfs site filters results on the same pages but also combining each query when a checkbox is selected, i think there is some ajax involved in achieving this also..

Link to comment
Share on other sites

Yes you just send the value though Http Request <input type='checkbox' onclick='stuff(100);' />, and have a div as the callback, in php just echo the result back.

 

function init() {
		if	(window.XMLHttpRequest){
		return new XMLHttpRequest();					
		}else if(window.ActiveXObject){
		return new ActiveXObject("Microsoft.XMLHTTP");
		}else{ 
		document.getElementById('err').innerHTML = "Your browser seems to be out of date, Please update!"; 
		return;
		}
}//end init 

function stuff(amount){ 

   	var req = init();

	req.open("GET",  "url.php?id="+amount, true);
	req.onreadystatechange = function() {
	    if (req.readyState == 4) {
	      if (req.status == 200) {
	          document.getElementById('callback_div').innerHTML = req.responseText;
	      }
	    }
	  };
	req.send();
}//end related

 

<span id='callback_div'></span>

Link to comment
Share on other sites

Thanks, il give that a try..should i create a seperate html file for checkboxes and include the php file or to keep it all as one, as i am currently using one php file and using echo to create the tables? possibly a dumb question but thought id ask  :-[

Link to comment
Share on other sites

yea true..i seem to have some part of it working so far..iv changed the GET url to one of my queries..e.g. price.php..this shows up whenever i click any checkbox option...Do i need to modify onclick='stuff(100); to the matching php file or am i completley missing something lol?  And also if more than one is selected will that combine the queries to narrow the results?

 

Thanks once again for all your help!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.