Praditha
Praditha

Reputation: 1172

How to load php file into div using jQuery Load with passing parameter

I've an element <div id="search_result"></div>, and then I used $.ajax to retrieve some data (search result).

$.ajax({
  url: "url to server",
  dataType: "json",
  data: keyword,
  type: "post",
  success: function(data){
    /* load searchResult.php with data as passing parameter to searchResult.php */
    $("#search_result").load("searchResult.php");
  }
});

but I wanna load those data of search result to searchResult.php, how it's it,.? and how I access those parameter in searchResult.php,.?

thanks,.

Upvotes: 2

Views: 4819

Answers (3)

Charaf JRA
Charaf JRA

Reputation: 8334

 var variable=10 ;
 $("#search_result").load("searchResult.php", {"variable": variable});

On searchResult.php :

 <?php echo $_POST['variable'];?>

Upvotes: 1

Rednas
Rednas

Reputation: 609

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

Upvotes: 1

karim79
karim79

Reputation: 342635

In your JS:

// ...
dataType: "json",

// pass key/value pairs
data: {keyword: "foobar"},
type: "post",
// ...

In your PHP:

if(isset($_POST['keyword']) && !empty($_POST['keyword'])) {
    echo $_POST['keyword']; // echoes "foobar"
}

Upvotes: 1

Related Questions