tags:

views:

53

answers:

4

I have a drop down menu with 3 values. each has an option id. I want to make an ajax request and update a div based on the response from that request.

I have sample code on jsbin in which I am trying to make ajax request to imdb. I don't want to parse the response back from imdb but want someone to show me how to update a div based on the response back from imdb. This is just a sample as I am trying to implement something like this in my application...only difference is that instead of making callback to imdb I'll be making callback to my applicaiton.

Please see the example

A: 

Something like:

$('select').change(function() {
    $('#yourdiv').load('to/your/page', {value: $(this).val()});
});

This will send a GET request (to/your/page?value=theSelectValue) whenever the value of the select element changes and load the response in the div with ID yourdiv.

Of course you have to adjust the selectors and the parameter name.

If you want to make a POST request, you can use the $.post() function with a callback:

$('select').change(function() {
    $.post('to/your/page', {value: $(this).val()}, function(data) {
        $('#yourdiv').html(data);
    });
});

Reference: .change(), .val(), .load(), $.post()

Felix Kling
I somehow feel as though you stole my answer. :-(
Chris
+2  A: 

First off, you will want to look into http://code.google.com/p/imdb-api/ or another api implementation to access IMDB information. This is a better approach compared to actually looking up a page on IMDB and parsing the html result.

This is how you could take your select and make a request to your server to use an IMDB API to lookup movie information:

 <script>
$(document).ready(function(){
  $("#drop-down").change(function(){
    $.ajax({
      type: "POST",
      url: "serverCodeToGetIMDBLookup.php",
      data: "movie="+$("#drop-down").val(),
      success: function(msg){
         //process return msg here with movie information 
      }
    });
  });  
 });
</script>

To merely get an IMDB page:

$("#drop-down").change(function(){    
  $('#result').load('someIMDBPage.html', function() {
     alert('Load was performed.');
  });
});
Chris
Thanks! so `success` is where I write something like `$('#my_div').html(msg);`
learn_plsql
@learn_plsql: No, this is already handled by `load()`: *load data from the server and place the returned HTML into the matched element.* So you would do: `$('#my_div').load('someIMDBPage.html')`
Felix Kling
A: 

Why don't u use the .ajax method from jQuery? Here's example:


$.ajax({
        type: "POST",
        url: "YOUR_URL",
        data: { query: $('input#query').val() }, //this is example only
        ontentType: "application/json; charset=utf-8",
        dataType: "json", //you can change to html
        success: function(result) { $('#div_id').html(result)},
        error: function(xhr, ajaxOptions, throwError) {
            alert(throwError);
        }
    });
Ventus
+1  A: 

User jquery's load

eg.

$("#drop-down").change(function(){
  $('#result').load('http://www.imdb.com/title/' + this.id + '/');
}); 
Sudhir Jonathan
`this.id` will give `drop-down`... is this what you mean?
Felix Kling
Lol! no, didn't notice that. But thats the example that's been cited in the question... used it so it would seem familiar.
Sudhir Jonathan