Solved

AJAX building query string from checkbox array

Posted on 2011-09-26
3
267 Views
Last Modified: 2012-05-12
Hi all,

I'm trying to build a querystring from a check box array.

Here's my AJAX code - the var options and var querystring are the areas I'm guessing I need to flesh out but not quite sure how to do it:

<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
			if(ajaxRequest.readyState == 4){
			document.getElementById('result').innerHTML = ajaxRequest.responseText;
		}
	}
	//Need to grab query string and append checked checkboxes array
	var options = document.get......
	var queryString = "?filter_options=" + options....;
	ajaxRequest.open("GET", "filter_query.php" + queryString, true);
	ajaxRequest.send(null);  
}

//-->
</script>

Open in new window



Here's some checkboxes:

<label for="1"><input type="checkbox" onClick="ajaxFunction();" name="filter_option[]" value="1" id="1">Option 1</label>
<label for="2"><input type="checkbox" onClick="ajaxFunction();" name="filter_option[]" value="2" id="2">Option 2</label>
<label for="3"><input type="checkbox" onClick="ajaxFunction();" name="filter_option[]" value="3" id="3">Option 3</label>

Open in new window


There can be loads of these and I was thinking of sending the string of id's to the server like:
filter_option=1-2-3-4-5-45-34-67-89 and doing an explode() on them before querying.

Lastly, the user arrives at this page with variables in the URL obtained via GET already.

e.g. search.php?qs=My+Search&date=3m


So I have a few questions around this:
A) How can this be achieved?
B) Is this the best way and is this fully browser compliant?
C) How can I include the variables already in the URL in the AJAX call? Write them into it via php?

Huge thanks
David
0
Comment
Question by:dolythgoe
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 350 total points
ID: 36600799
use :

var checkboxes = document.getElementsByName("filter_option[]");
var queryString = [];
for(var i=0;i<checkboxes.length;i++) {
    if( checkboxes[i].checked ) {
        queryString.push( checkboxes[i].value );
    }
    
}
queryString = "?filter_options=" + queryString.join(",");

Open in new window


test page : http://jsfiddle.net/XekX7/
0
 
LVL 3

Assisted Solution

by:azserrata
azserrata earned 150 total points
ID: 36603584
In order to include the "variables already in URL" into the new URL you could add to leakim971 last line:

prevQS="<?php echo($_SERVER["QUERY_STRING"])?>";
queryString = "?"+prevQS+"&filter_options=" + queryString.join(",");

Open in new window


I believe this should work.

Cheers
David
0
 

Author Closing Comment

by:dolythgoe
ID: 36711193
Huge thanks guys
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How many transactions can mysql handle? 3 44
MS SQL Server - Looking to filter rows based on column value 3 48
How to fix Datetime in MySQL? 4 51
Currency in SQL? 2 32
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question