1

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 ?

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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);
      }
    });

3 Comments

this is possible with .load function.
both are possible ,but ajax is better option,best practice.
Both methods are working. But I've opted for the jquery. Thanks alot
0

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" ] } );

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.