How do I do XMLHttpRequest for a link with http_build_query function in PHP

Hi guys,

I have a link with structure which look like this:

<a href="index.php?<?php echo http_build_query(['x' => [$a,$b]]); ?>">Get Data</a>

I want to do ajax request which would look something like this:

var xhr = new XMLHttpRequest();
xhr.open('GET', 'index.php?' + [x =>[var_a,var_b]], true);
xhr.send();
xhr.onreadystatechange = function () {
	if (xhr.readyState === 4) {
		var msg = JSON.parse(xhr.responseText);
	}
}

Is it possible to get this done.

Thank you,

you can use http_build_query() only in PHP. so only if the values for the URL already exist at PHP’s runtime then you can use it.

Thank you,

I get it done with something like this:

xhr.open('GET', 'index.php?x[0]=' + var_a + '&x[1]=' + var_b, true);

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.