Load content using jquery along with passing a variable to it

I have a variable - xin jquery, I load the php part into a block div, and I need to send this variable to it.

So -

$(document).on('click','#link',function () {
var x=5;
$("#block").load("file.php");
});

As a result, I'm still on the same page. I just need to have it variable x.

I am thinking about using the GET method or XMLHttpRequest, but I do not want to use another page, I just need to load the php part into a block.

+4
source share
4 answers

One solution is to send data as a second argument:

$(document).on('click', '#link', function() {
  $("#block").load("file.php", {
    x: 5
  }, function(res) {
    //your callback 
  });
});

References

.load ()

+6
source

GET URL-, , . , .

var x=5;
$("#block").load("file.php?extra_parameter=" + x);

PHP x $_GET "" :

$x = $_GET["extra_parameter"];
+4

Pass the value of the variable through the Get parameter as follows:

$("#block").load("file.php?variable_name=" + x);
+1
source

A bit late for the party, but I will post it anyway: Try jQuery.ajax (), this will allow you to send your data as POST

$.ajax({
  type: "POST",
  url: url,
  data: data, //The variables you want to send through
  complete: function(obj, status) {
      //Here you can take the response from the request and load
      //it into the current page
  },
  dataType: dataType //What you expect the format of the returned
                     //data will be, usually html or json
});

Link: http://api.jquery.com/jquery.ajax/

If this seems too complicated, there is also a wrapper for everything: http://api.jquery.com/jquery.post/

0
source

All Articles