Good morning friends, I have these buttons here:
<button class="btn-excluir" data-id="1">Primeiro</button>
<button class="btn-excluir" data-id="2">Segundo</button>
<button class="btn-excluir" data-id="3">Terceiro</button>
<button class="btn-excluir" data-id="4">Quarto</button>
<button class="btn-excluir" data-id="5">Quinto</button>
And this simple script:
<script type='text/javascript'>
var btn = document.getElementsByClassName('btn-excluir');
for (var i = 0; i < btn.length; i++) {
btn[i].addEventListener('click', function (e) {
var a = e.target.dataset.id;
ajax_envio('script.php?variavel=' + a);
}, false);
}
function ajax_envio(arquivo) {
var http = new XMLHttpRequest;
http.open('GET', arquivo, true);
http.send();
}
</script>
The PHP code, to get the value of the variable:
<?php $valor_da_variavel = $_GET['variavel'];
echo "Valor da variável: $valor_da_variavel"; ?>
The script basically takes the data-id button and sends this value to the variable $ variable_value .
But here's the problem: URL does not want to change, although when I press F12 and I go to NETWORK and I click one on some button, it shows in the Name column what it should look like in URL . It basically looks like this:
Name: script.php? variable = 2 (if I click the second button)
Status: 200 OK
Type: xhr
Initiator: .php: 72
Size: 2.5Kb
Time: 10ms
And as I'm using $ variable_value as a parameter in a function, it's returning null.
I wonder what the problem might be. As for PHP. the error message I have is:
Undefined index: variable_value (What is the $ variable_value);
Thank you in advance !!