4

I have a variable - x in jquery, I am loading a php part in div block and I need sent that variable to it.

So -

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

In result I am still in the same page.I just need to have variable x in it.

I think about to use GET method or XMLHttpRequest but I don't want to another page, I just need to load a php part in block.

4 Answers 4

6

One solution is to send data as second argument:

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

References

.load()

6
  • 2
    +1 A much more elegant solution to use the load parameters rather than construct a URL string like I did.
    – Lix
    Jun 1, 2015 at 7:26
  • Ooo that is interest variant
    – ZeroVash
    Jun 1, 2015 at 7:29
  • 1
    Glad that I helped you. Also thanks @Lix for the positive feedback.
    – Alex Char
    Jun 1, 2015 at 7:52
  • just out of topic but in result I use $i= stripslashes(strip_tags($_POST['x'])); in reault gave me .5. Do you know how to erase the dots from both sides that i can use the variable as a number
    – ZeroVash
    Jun 1, 2015 at 7:56
  • $_POST['x'] that seems php to me that I'm not good at. You can post another question related in php how to fix this issue.
    – Alex Char
    Jun 1, 2015 at 7:59
4

You could append a GET parameter to the URL you are loading and then within that file you can insert your variable in the appropriate place. There are no redirections, or naviagations to other pages, the variable will be passed as part of the request for the content.

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

Your PHP file will then have access to the x variable via the $_GET 'superglobal' variable:

$x = $_GET["extra_parameter"];
1
  • 1
    @alex_mike - consider using the method in Alex Char's answer, I feel it is much more readable and also will allow you to extend the number of variables very easily.
    – Lix
    Jun 1, 2015 at 7:27
1

Pass the variable value via Get parameter like the following:

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

Bit late to the party, but I'm going to post it anyway: Try out Jquery.ajax(), it 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
});

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

If that seems like too much trouble, there's also a wrapper for all that: http://api.jquery.com/jquery.post/

2
  • Take a look at the docs for the .load() function, it is actually shorthand for the .$.get function. Furthermore, as detailed in this answer, the .load() function also supports a data property that acts the in the same way as the data property in a call to $.ajax.
    – Lix
    Jun 1, 2015 at 14:07
  • Yeah, there's a couple of wrappers available, but I think it's better to stick with the one closer to the core, as it gives a lot more extensibility. The wrappers are limited to certain very specific use conditions, for instance, the .load() function doesn't support defining a dataType. This might seem trivial, but I've seen it many times where I json_encode something sent through the backend, and the browser reads it as html for some reason.
    – StormBeast
    Jun 1, 2015 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.