Solved

Returning values with AJAX

Posted on 2011-10-01
7
409 Views
Last Modified: 2012-05-12
Hello all,

I have setup an AJAX call to a php file to run a query and spit out some results - all good.

Now, what I'm looking to do is feed some of those results into an array and send it back to the original page that made the call for processing via javascript.

I've read about encoding to json and then decoding that via javascript.

So here's some code:

<script type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
	var ajaxRequest; 
	//Show loading graphic
	document.getElementById('loading').style.display="block";
	
	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("A problem has occured with your request");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
			if(ajaxRequest.readyState == 4){
			document.getElementById('loading').style.display="none";
			document.getElementById('search_results').innerHTML = ajaxRequest.responseText;
		}
	}
	//Need to grab query string and append checked checkboxes array
	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 );
		}
		
	}
	prevQS="<?php echo($_SERVER["QUERY_STRING"])?>";
	queryString = "?"+prevQS+"&filter_options=" + queryString.join(",");
	ajaxRequest.open("GET", "filter_query.php" + queryString, true);
	ajaxRequest.send(null);  
}

//-->
</script>

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

Open in new window



So if a checkbox is clicked, it's id fires over a string of id's via GET to the filter_query.php which works some magic and outputs results.

What I want to now do, is get the php script to send an array back to javascript - an array of checkbox ids that I would use to then disable those checkboxes. This is kind of like showing the user what options are now appliceable after they've picked one option - narrowing them down.

Any help, hugely appreciated as usual.

Cheers
David


0
Comment
Question by:dolythgoe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 200 total points
ID: 36897856
>an array of checkbox ids that I would use to then disable those checkboxe

end of your php script :

echo "12,33,44,55,66";

?>

Open in new window


and client side use :

var myarray = ajaxRequest.responseText.split(",");
0
 

Author Comment

by:dolythgoe
ID: 36897861
Thanks for that but a lot more HTML is spat out in the result so wouldn't this try to split all the response HTML?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36897881
no, an ajax response should contains only the required data...
if needed make two ajax calls
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dolythgoe
ID: 36897953
Ah ok, can I do 2 calls with what I have above? I do I check for the response from call 1 or call 2? Thanks!
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36897957
successive ajax call, not at the "same" time
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 300 total points
ID: 36898207
Something to look out for... In PHP, JSON can confuse arrays and objects.  Example:
http://www.laprbass.com/RAY_temp_dolythgoe.php
<?php // RAY_temp_dolythgoe.php
error_reporting(E_ALL);
echo "<pre>" . PHP_EOL;

// SEE http://php.net/manual/en/function.json-decode.php

// CREATE SOME TEST DATA
$arr = array
( 'pink'  => 'Happy'
, 'blue'  => 'Sad'
, 'green' => 'Kermit'
)
;

// SHOW THE ARRAY
var_dump($arr);

// MAKE A JSON STRING FROM THE ARRAY
$json = json_encode($arr);
var_dump($json);

// TRY TO RECOVER THE ARRAY FROM THE JSON STRING... OOPS!
$new = json_decode($json);
var_dump($new);

// DID WE GET THE RIGHT DATA BACK?  NOPE.
if ($new ==  $arr) echo PHP_EOL . "Success!! We got a matching array";
if ($new === $arr) echo PHP_EOL . "Success!! We got an identical array";

// TRY IT AGAIN USING THE SECOND ARGUMENT
$new = json_decode($json, TRUE);
var_dump($new);

// DID WE GET THE RIGHT DATA BACK THIS TIME?  YEP.
if ($new ==  $arr) echo PHP_EOL . "Success!! We got a matching array";
if ($new === $arr) echo PHP_EOL . "Success!! We got an identical array";

Open in new window

0
 

Author Closing Comment

by:dolythgoe
ID: 36900017
Thanks guys
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

738 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