Kurt Jarchow's Blog

January 28, 2009

Add Drupal Forms using AJAX

Filed under: Uncategorized — Tags: , , — Kurt Jarchow @ 12:05 pm

Something very easy to do in drupal is adding form object to the content of your page without doing a page refresh.  I needed to add functionality so that an application form was displayed to a user when they clicked the Apply link.

Using JQuery we can “intercept” a client click of a link.  Add a .js file to which ever module you are editing, and add the following:

 if (Drupal.jsEnabled) {  

  $(document).ready(function () {
    $(’a.apply-link’).click(function () {

      var submitApplication = function(data) {
        $(’div.apply-link’).html(data.html);
      }

    $.ajax({
      type: ‘POST’,
      url: this.href,
      dataType: ‘json’,
      success: submitApplication,
      data: ‘js=1′
    });
    return false;
  });
  });
}
So when a link with a class “submit-application” is clicked an ajax/json call is made to the url.  Now make sure the js is included somewhere in your module code by using drupal_add_js(). 
Now all we have to do is make sure the page renders as json if an ajax call is made.  Add this  code just before the _page exits (but not after the exit(); !).
 if (!empty($_POST['js'])) {
    $html = drupal_get_form(’job_posting_application_form’, array($node->nid, $node->title, $node->job_posting_email));
    drupal_json(array(
    ‘html’ =>  $html
    )
    );
Your link should be replaced with the form.
If you want to add a little bit of animation use the jQuery animation options.  Instead of just replacing the link, make it fade in:
$(’div.apply-link’).hide();
$(’div.apply-link’).html(data.html);
$(’div.apply-link’).fadeIn();
Credit to John K. VanDyk’s book for breaking the back of this code.

November 19, 2008

Building a Multinational Website

Filed under: Uncategorized — Tags: , , , — Kurt Jarchow @ 10:28 pm

There is nothing easy about building a multiple-location, multi-lingual website.  The options are getting better and better however to make this as smooth as possible.

I’ve been blogging about my experiences, starting from scratch, learning the LAMP stack and delivering a high quality, high performance solutions.  I’ve had my head in a few books lately; a php beginners book, a php advanced book, and a Drupal Pro book.  My eyes are heavy but I have a prototype to my end solution.

Building a solr search driven search site is completely painless, and getting faceted search options are completely customizable.  Out of the box, Drupal gives our website about 60% of our functionality.

However, be prepared: Drupal is a whole other language unto itself.  Coming from a nice SDK an a .Net environment, the vast number of function names you need to remember is quite dawnting.  .Net feels much cleaner- that being said I think I can do much more with the php code.

My next steps will be mastering the Jquery integration.  This site needs to feel real 2.0, and I plan on keeping up with key AJAX function.

That’s it for me tonight, time for some sleep.

Powered by WordPress

Bad Behavior has blocked 103 access attempts in the last 7 days.