My project requirement is to have a dependent select drop down box that will get populated based on another select box selection.
I want to use ajax. Anyone has any update.

Comments

mandarmbhagwat78’s picture

Hay, I got the solution. Here is the code

samplemodule.info

; $Id$
name = "Sample module"
description = "Sample module."
package = "SampleProject"
core = 6.x
php = 5.1

samplemodule.module

<?php
// $Id$

/*
* Provides admin facility to create free profiles for privileged users.
* @file
*/

/**
* Implementation of hook_help()
*/
function samplemodule_help($path, $arg)
{
	if($path == 'admin/help#samplemodule')
	{
		$txt = "dependent drop down sample.";
		$replace = array();
		return '<p>'. t($txt, $replace). '</p>';
	}
}

/*
* Implementation of hook_menu()
*/
function samplemodule_menu()
{
	$items['admin/settings/samplemodule'] = array(
      'title' => 'Dependent dropdown',
      'description' => t('Dependent dropdown.'),
      'page callback' => 'drupal_get_form',
	  'page arguments' => array('samplemodule_form'),
	  'access arguments' => array('administer site configuration'),
      );
	$items['content_js/%'] = array(
		'access arguments' => array('access content'),
		'page callback' => '_content_fill',
		'page arguments' => array(1),
		'type' => MENU_CALLBACK,
	  );
	return $items;
}

function _content_fill($ntype)
{
	drupal_set_header('Content-Type: text/plain; charset: utf-8');
	$res = db_query("SELECT nid, title FROM {node} WHERE type = '%s' and status = 1", $ntype);
	while($record = db_fetch_object($res))
	{
		$contents .= $record->nid . ":" . $record->title . " , " ;
	}
	print $contents;
}

/*
* Form creation
*/
function samplemodule_form()
{
	$form = array();
	
	$content_type = node_get_types('types');
    drupal_add_js(drupal_get_path('module', 'samplemodule'). '/content_autocomplete.js');
	$form['content_type'] = array(
		'#weight' => -10,
		'#type' => 'select',
		'#title' => t('Content Type'),
		'#required' => TRUE,
		'#options' => $content_type,
        '#allowed_values' => $content_type,
        '#attributes' => array('onchange' => 'fillContent(this.value);'),
	);
	$form['content'] = array(
		'#weight' => -9,
		'#type' => 'select',
		'#title' => t('List of Content'),
		'#required' => TRUE,
		'#options' => array(''),
		'#allowed_values' => array(''),
	);

	$form['submit'] = array(
                '#type' => 'submit',
                '#value' => t('Submit'),
	            );
	$form['#submit'] = array('samplemodule_form_submit');
  return $form;
}

function samplemodule_form_alter(&$form, $form_state, $form_id) 
{	
	if($form_id == 'samplemodule_form')
	{
		$form['content']['#allowed_values'] = _content_list($form_state['post']['content_type']);
		$form['content']['#options'] = _content_list($form_state['post']['content_type']);
	}
}

function _content_list($ntype)
{
	$res = db_query("SELECT nid, title FROM {node} WHERE type = '%s' and status = 1", $ntype);
	$records = array();
	while($record = db_fetch_object($res))
	{
		$records[$record->nid] = $record->title;
	}
	return $records;
}

function samplemodule_form_submit($form, &$form_state)
{
	db_query("INSERT INTO {samplemodule} (nid, ntype) VALUE(%d, '%s', unix_timestamp('%s'), unix_timestamp('%s'))", $form_state['values']['content'],  $form_state['values']['content_type']);
	drupal_set_message($form_state['values']['content_type'] . '  featured successfully.', 'status', FALSE);
}

samplemodule.install

<?php
// $Id$

/*
* Install the samplemodule module, including it's content (node) type
* @file
*/

/*
* Implementation of hook_schema()
*/
function samplemodule_schema()
{
	$schema['samplemodule'] = array(
		'fields' => array(
            'nid' => array(
                'type' => 'int',
                'unsigned' => TRUE,
                'not null' => TRUE,
                'default' => 0,
            ),
            'ntype' => array(
                'type' => 'varchar',
                'length' => 127,
                'not null' => TRUE,
                'default' => '',
            ),			
		),
		'indexes' => array(
			'nid' => array('nid'),
		),
	);
	return $schema;
}

/*
* Implementation of hook_install()
*/
function samplemodule_install()
{
	drupal_install_schema('samplemodule');
}

/*
* Implementation of hook_uninstall()
*/
function samplemodule_uninstall()
{
	drupal_uninstall_schema('samplemodule');
}

content_autocomplete.js

var xmlhttp=null;
function fillContent(ntype)
{
	if (window.XMLHttpRequest)
	  {
	  // code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else if (window.ActiveXObject)
	  {
	  // code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	else
	  {
	  alert("Your browser does not support XMLHTTP!");
	  return;
	  }
	 // alert(ntype);
	var url="http://localhost/drupal-6.10/content_js/" + ntype;
	xmlhttp.open("GET",url,true);
	//xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = handleRequestStateChange;
	xmlhttp.send(null);
}
			
// function executed when the state of the request changes
function handleRequestStateChange() 
{
	// continue if the process is completed
	if (xmlhttp.readyState == 4) 
	{
		// continue only if HTTP status is "OK"
		if (xmlhttp.status == 200) 
		{
			response = xmlhttp.responseText;
			if(!response.length) return;
			var contentsctl = document.getElementById('edit-content');
			var contents = response.split(",");
			contentsctl.options.length = 0;
			for(i=0; i<contents.length-1; i++)
			{
				var content = contents[i].split(":");
				contentsctl.options[contentsctl.options.length] = new Option(content[1], content[0]);
			}
		}
	}
}
javier.ortiz.llerena’s picture

Hi mandarmbhagwat78,
I have been trying to make work your module example with no success, first of all I get a
funny black row at the top of my screen (over the logo block) once I install this module
and an "Apache HTTP server stop working" message if I try to access the (...)/admin/settings/samplemodule
page.

Could you please let me know where did you find any useful information or give me some
clues about what am I doing wrong? Thank you.

Javi.

PS: I tried that on Firefox, IE and Chrome.

jcisio’s picture

valthebald’s picture

Status: Active » Closed (won't fix)

This does not belong to core