0
Good night,
I am trying to send a simple code array (2,5,7,etc) in Json format to a php page through an Ajax request but I cannot.
My JS:
<script type="text/javascript">
function enviArray() {
cod_jogador.sort();
var meuArray = JSON.stringify(cod_jogador);
var x;
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
httpRequest.open("GET", "selecao.php", true);
httpRequest.send("x=" + meuArray);
};
</script>
The cod_jogador
is the vector with the values.
My HTML:
<div id="demo">
<button type="button" name="" value="" class="button" onclick="enviArray()">Teste</button>
</div>
On the page selecao.php
i have the variable created to receive JSON:
$array = json_decode($_GET["x"], false);
echo "$array";
But whenever I echo in any variable in the GET
(I’ve tried with POST
), it always returns me indefinite. What I’m doing wrong?
Oliveira, thank you very much Kra. It worked here. I was days ago looking for this solution, saved me. Great hug and may God pay you
– Diogo Barreto