Sending problem via URL (GET)

1

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 !!

    
asked by anonymous 28.09.2018 / 14:38

2 answers

0

Talk to me! Is that okay?

I tested the code on my machine and after including the script to load along with the page "window.onload" it worked normally. While modifying the URL that appears in the Ajax browser it does not make this change since it makes asynchronous requests by default. I recommend giving in this material about how ajax works.

To see if the php script is returning the correct information, inspect the page and check the network (selecting the request) and the "Response" tab. Same in the image below.

    
28.09.2018 / 15:48
0

As for the problem, I ended up doing it in a simpler and better way (I really do not know if it's better because I do not know the difference practically):

Instead of <button></button> , I used <a></a> , and I put href"script.php?variavel=2" , and it worked.

So the variable took the value normally and worked. However, it only worked when it was out of function, inside function it seems that the capture of the variable does not work, I do not know why. But I took it off, and it turned into jewelry.

    
28.09.2018 / 16:42