jonah
jonah

Reputation: 213

ajax load an external file by passing parameters

Am trying to load an external file to a div using this function

  $(document).ready(function(){
   $("#postdiv").load('posts.php');
       });

This is working alright.

The problem is, I need to pass parameters/variables to posts.php from the caller page and use them to do some filtering.

How can i do this ?

Upvotes: 1

Views: 2826

Answers (3)

Neel
Neel

Reputation: 11731

you can make an ajax call

$.ajax({
      url: "posts.php",
      data: data,
      type: "post",
      success: function(data){
            $('#postdiv').html(data);
      }
    });

or you want to go for load then try below code

$( "#postdiv" ).load( "posts.php", { "test[]": [ "test1", "test2" ] } );

Upvotes: 0

Pratik Joshi
Pratik Joshi

Reputation: 11693

Use ajax

ajax is better option,best practice.

  var value = "value of the data here";  
    $.ajax({
      url: "posts.php",
      data: "key="+value,
      type: "post",
      success: function(data){
            $('#postdiv').html(data);
      }
    });

Upvotes: 1

Nauphal
Nauphal

Reputation: 6202

You can pass parameters with jquery load

This method will pass parameter as POST

$("#postdiv").load('posts.php',{'name' : 'Test','age' : 25});

if you want pass it as GET you can do like this

$("#postdiv").load('posts.php?name=Test&age=25');

you can read more here

Upvotes: 2

Related Questions