How to return an array from php using ajax and json?

Steven

New member
I am making a program that deals cards and basically i have to use ajax to call a php file to do so, the php file will be randomly generating 4 numbers between 1-52 which will be my cards, how can i pull the values from this php file and store them in variables to be used by a javascript function? I know i will probably need to use json encode to get the variables from php to the javascript but thats about it. I only have about this much progress on the ajax part and can't quite figure out how to doit after searching the internet for quite a while, also the ajax only needs to be compatible with firefox.

head>
<script type="text/javascript">
function randomdeal()
{
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","randomdeal.php?",true);
xmlhttp.send();
}
</script>
</head>

I know how to do all the coding for generating the numbers and such and storing them in an array to return in the php file but i just cant figure out how to return the actual array so that i may pull the 4 cards needed out and use them in my javascript. Any help would be greatly appreciated. Most tutorials i find on the internet just output directly from the php file called using ajax to the screen.
 
Back
Top