Search code examples
phpjqueryajaxrequestreferenceerror

PHP variable inside AJAX data string


I get Uncaught ReferenceError: bierta (or whatever the URL var is) is not defined when I run the AJAX call below. How can I fix this?

$.ajax({
  type: "GET",
  async: false,
  url: "get-single-marker.php",
  data: "slug="+<?php echo $_GET['gt']; ?>,
  dataType: "json",
  success: function(res) {
    data = res;
  }
});

Solution

  • Try

    data: "slug=<?php echo $_GET['gt']; ?>,
    

    or

    data:{slug:"<?php echo $_GET['gt']; ?>"},