Returning values with AJAX

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


dolythgoeAsked:
Who is Participating?
 
leakim971Connect With a Mentor PluritechnicianCommented:
>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
 
dolythgoeAuthor Commented:
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
 
leakim971PluritechnicianCommented:
no, an ajax response should contains only the required data...
if needed make two ajax calls
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
dolythgoeAuthor Commented:
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
 
leakim971PluritechnicianCommented:
successive ajax call, not at the "same" time
0
 
Ray PaseurConnect With a Mentor Commented:
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
 
dolythgoeAuthor Commented:
Thanks guys
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.