views:

792

answers:

3

Hello, I am attempting to use AJAX in jQuery to load content to a div, however, it is severely failing:

Here's the javascript:

$(document).ready(function() {
  $('#webdev').hide();
     $("#apply-webdev").click(function() {
        var form = $("#webdev");
        var formContent = form.find("#webdev");
        form.slideToggle();
        $.ajax({
            url: "api.php?do=get_form_webdev",
            cache: false,
            success: function(data) {
                form.html(data.params);
        },
        dataType: "json"
      });
  });
});

And here's the HTML:

<div class="rbutton"><button title="Apply for position" id="apply-webdev" onclick="load_webdev_form()">&nbsp;Apply&nbsp;</button></div>
 <div id="webdev">
    <fieldset><legend>Apply for position</legend><div style='padding:10px; text-align:center'><img src='/images/load.gif'/></div></fieldset>
 </div>

What am I doing wrong?

EDIT

Below is the new code, based on answers given in this thread:

$(document).ready(function() {
    $('#webdev').hide();
    $("#apply-webdev").click(function() {
  $("#webdev").slideToggle();
        $("#webdev").load("api.php?do=get_form_webdev");
    });
 $('#webdevcancel').click(function()
  {
   $('#webdev').hide('slow');
  }
 );
 $('#webdevsave').click(function()
  {
  $('#webdev').block({ 
        message: '<h1>Processing...</h1><img src="/images/load.gif" /><br /><br />', 
        css: { border: '3px solid #a00' } 
    });
  }
 );
});
A: 

In the success function below, shouldn't you be using form.html(data.params) instead of formContent.html(data.params)?

success: function(data) { formContent.html(data.params); },

mga911
fixed, but still not loading the external content
Shamil
+1  A: 

The easier way to load content into an element in jQuery is the load method:

$("#webdev").load("api.php?do=get_form_webdev");
AKX
not working either :/
Shamil
are you sure that visiting api.php?do=get_form_webdev get you any result, can you give the full url here. it's strange for load function not to work
Bassel Safadi
currently, it's pulling the stuff off, http://thestudentroom.com/api.php?do=get_widget_customize_all, however, for purposes, I have used "api.php?do=get_form_webdev". http://thinkteen.co.uk/volunteer is where all of this is happening in the end.
Shamil
A: 

What do you mean by "it is severely failing"?
I'd tried to set the dataType to html and use the data without the params like form.html(data.);

chchrist
see new code, revised :)
Shamil