Simple Drupal Ajax Forms (version 0.2)

The following code is a follow-up of the previous article. The code below can be used as stand-alone, without the download of the framework, AND is capable of doing multiple requests at the same time. This was something the previous code was unable to do (due to framework limitations).

function printAjaxJs() {
  $output='function getHTTPObject() { var xmlhttp; if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { try { xmlhttp = new XMLHttpRequest(); } catch (e) { xmlhttp = false; } } return xmlhttp;}';
function printAjaxFunction($function,$page) {
  $output.="function ".$handle."() { if (".$http.".readyState == 4) { if (subject_id != '') { document.getElementById(subject_id).innerHTML = ".$http.".responseText; } } }";
  $output.='var '.$http.' = getHTTPObject();';
  $output.='function '.$function.'(div_id,content_id) {';
  $output.='subject_id = div_id;';
  $output.='content = document.getElementById(content_id).value;';
  $output.=$http.'.open("GET", "'.$page.'" + content, true);';
  $output.=$http.'.onreadystatechange = '.$handle.';';

3 thoughts on “Simple Drupal Ajax Forms (version 0.2)

  1. Hi, I arrived here looking for something I need to work with Drupal forms. I like your code, as other examples I found on internet, but I think it doesn’t reach that goal. Maybe you can help me with that.

    What I’m trying to do is something like Gmail’s “attach another file” link, I mean a textbox and the possibility to add so many other equal textboxes as you want. It’s easy with your code, but you know that Drupal forms have a lot of php relevant info on its fields definition, what makes it possible getting the data and saving it into the database. When you create textboxes on the client side, all this information can’t be told to drupal, so new textboxes doesn’t have a “name” and Drupal won’t get info from its submitted data (or I don’t know how to get it).

    Any ideas?



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.