0

This is driving me crazy...

$(document).on("click",".load",function(b){
  b.preventDefault();
  $.get("/url/get.php",{
    id: 123
  },
  function(a){
    $("#result").html(a);
  })
});

This loads the page as expected but when I do print_r($_GET) it shows it's empty... Any ideas?

Backend:

 if (isset($user) == false) {
     session_start();
     $path_to_root = "../";
     require($path_to_root."require/loads.php");
     $PDO = new PDO(DB_CONN, DB_USERNAME, DB_PASSWORD);
     $user = new User($PDO);
 }
 $i = 0;
 print_r($_GET);
Micael Dias
  • 331
  • 2
  • 12

3 Answers3

0

Please, try this way, using done() function:

$(document).on("click",".load",function(b){
  b.preventDefault();
  $.get("/url/get.php",{
    id: 123
  }).done(
  function(a){
    $("#result").html(a);
  })
});

As said in jquery docs that´s the way to send payload data with GET.

Anyway, you can also use ajax:

$.ajax({
  url: "/url/get.php",
  type: "get", //send it through get method
  data:{id:123},
  success: function(response) {
      $("#result").html(a);
  },
  error: function(xhr) {
    $("#result").html("ERROR");
  }
});

Anyway, if you are still viewing those errors, the problem souhld be in your backend as @AmmarCSE commented.

Hope it helps

EDIT some text about the difference between jquery methods success() and done() :

jQuery ajax success callback function definition

Community
  • 1
  • 1
0

please try this

 $(document).on("click",".load",function(b){
  b.preventDefault();
   $.ajax({
   method:'get',
   url : '/url/get.php', 
   data : '{id:123}',
   dataType: 'json', //json,html any you will return from backend
   success:function(a){
       $("#result").html(a);
     }


   })
 });
Shreeraj Karki
  • 234
  • 2
  • 11
0

Try using $data = file_get_contents("php://input");

$r = json_decode($data);

instead of $_GET in you php file, then print $r

Alpesh Trivedi
  • 944
  • 2
  • 13
  • 33