How can I get a URL parameter with jQuery?

Viewed 1230

I am sending an AJAX request and want to send the data of the URL variable with it. I have tried using PHP's GET, however it is not sent.

$.ajax({
  type: "POST",
  url: "send.php",
  data: "name=" + "<?php $_GET['name']; ?>", 
  success: function(data) {
    $("#div").html(data);
  }
})
3 Answers

you need to echo the get variable.

$.ajax({
       type: "POST",
       url: "send.php",
       data: "name=" + "<?php echo $_GET['name']; ?>", 
       success: function(data) {
           $("#div").html(data());
       }
})

moreover, its prefer you use the object to send data.

$.ajax({
       type: "POST",
       url: "send.php",
       data: {'name':'<?php echo $_GET['name']; ?>'}, 
       success: function(data) {
           $("#div").html(data());
       }
})

There are two things.

$.ajax({
   type: "POST",
   url: "send.php",
   data: "name=" + <?php echo $_GET['name']; ?>, 
   success: function(data) {
     $("#div").html(data());
   }
});

First is <?php echo $_GET['name']; ?>.

Second is missing closing bracket $("#div").html(data());

Hope this will help you.

$.ajax({
  type: "POST",
  url: "send.php",
  data: "name=" + <?php echo $_GET['name']; ?>, 
  success: function(data) {
    $("#div").html(data();
  }
});
Related