How to pass the dynamic variables from a url to php via ajax

2

First of all, I have already looked at several other posts about the subject here and I did not find what I need, my scenario is the following I am generating several EX links:

challenge.php? id = 2 & name = joao

challenge.php? id-3% name = jose

And I need to pass this via ajax to my php page which processes this. But the page is not picking up the data, what can it be? follow my code

urls generated by <a id='desafiar' href='desafiar.php?idd={$linha['id']}&d={$linha['username']}'></a>

script

 $(document).ready(function() {
    $("#desafiar").click(function( e ) {
            e.preventDefault();
            var url = $( this ).attr('href');
            $.ajax({
                 cache: false,
                type: "POST", 
                 url: "desafio.php", 
                data: {url},
                success: function( data ){
                $("#resultado").html( data );
                }
              });
    });
 });

on the page that takes php

  $iddesafiante = $_SESSION['user_id'];
  $desafiante = $_SESSION['username'];
  $iddesafiado = $_GET['idd'];
  $desafiado = $_GET['d'];

open to suggestions

    
asked by anonymous 13.02.2016 / 03:24

1 answer

3

You are using type: "POST", in JavaScript and $_GET in PHP. You have to use the same in both.

If you are not using the link from this anchor then I suggest you use another way to pass these values. You can have json in HTML like this:

<a id='desafiar' data-idd="{$linha['id']}" data-d="{$linha['username']}" href='desafiar.php'></a>

So it makes more sense to me, and then you can use

$(document).ready(function() {
     $("#desafiar").click(function(e) {
         e.preventDefault();
         var data = this.dataset;
         $.ajax({
             cache: false,
             type: "GET",
             url: "desafio.php",
             data: data,
             success: function(data) {
                 $("#resultado").html(data);
             }
         });
     });
});
    
13.02.2016 / 08:57