Hi all,

I am trying to get a complex piece of code to work but I just can't get my head around it. This references the following 2 posts that I have made before:

- http://drupal.org/node/367524
- http://drupal.org/node/358356

What I want is basically the same as the above two posts. I need the code, when I select a country I want it to populate a province select field with only the provinces that is part of that country. Up to this point I can do. Now the problem is, when I select a province I need for it to basically create a new select field with all the towns related to this province. I cannot for the life of me figure out how to make the second function fire once it has been selected.

The code I have is this :

function module_estab_menu()
{
	$items['module_estab/message_js'] = array(
		'page callback' => 'module_estab_message_js',
		'access arguments' => array('access content'),
		'type' => MENU_CALLBACK,
	);
	
	$items['module_estab/message1_js'] = array(
		'page callback' => 'module_estab_message1_js',
		'access arguments' => array('access content'),
		'type' => MENU_CALLBACK,
	);
	
	return $items;
}

function module_estab_message_js() 
{
	$form = array(
		'#type' => 'select',
		'#title' => 'You selected that because...',
		'#options' => array(
			'1' => 'drugs',
			'2' => 'I do what I want.',
			'3' => "I'm feeling lucky..."
		),
		'#ahah' => array(
			'event' => 'change',
			'path' => 'module_estab/message1_js',
			'wrapper' => 'target1',
			'method' => 'replace',
			'effect' => 'fade',
		),
	);

	print_r($form);
	$output = ahah_render($form, 'user_problem');
	print drupal_to_js(array('data' => $output, 'status' => true));
	exit();
}

function module_estab_message1_js() 
{
	$form = array(
		'#type' => 'select',
		'#title' => 'You selecRRRse...',
		'#options' => array(
			'1' => 'drugs',
			'2' => 'I do what I want.',
			'3' => "I'm feeling lucky..."
		),
	);

	$output = ahah_render($form, 'user_problem_one');
	print drupal_to_js(array('data' => $output, 'status' => true));
	exit();
}

function ahah_render($fields, $name) 
{
	$form_state = array('submitted' => FALSE);
	$form_build_id = $_POST['form_build_id'];
	// Add the new element to the stored form. Without adding the element to the
	// form, Drupal is not aware of this new elements existence and will not
	// process it. We retreive the cached form, add the element, and resave.
	$form = form_get_cache($form_build_id, $form_state);
	$form[$name] = $fields;
	form_set_cache($form_build_id, $form, $form_state);
	$form += array(
		'#post' => $_POST,
		'#programmed' => FALSE,
	);
	// Rebuild the form.
	$form = form_builder($_POST['form_id'], $form, $form_state);


	// Render the new output.
	$new_form = $form[$name];
	return drupal_render($new_form); 
}

function module_estab_add_form($form_state)
{
	$options = array('1' => t('Enabled'), '0' => t('Disabled'));
	$form['e_details'] = array(
		'#type' => 'fieldset',
		'#title' => t('Establishment Details'),
		'#collapsible' => TRUE,
		'#collapsed' => FALSE,
		'#tree' => TRUE,
	);
	
	$temp = array();
	
	$sql ="SELECT * FROM db.l_country";
	$result = mysql_query($sql) or die("Query failed : " . mysql_error());
	
	while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		$cc_id = $line['cc_id'];
		$cc_name = $line['cc_name'];
		$temp[$cc_id] = $cc_name;
	}
	
	$form['e_details']['e_country'] = array(
		'#type' => 'select',
		'#title' => t('Country'),
		'#options' => $temp,
		'#ahah' => array(
			'event' => 'change',
			'path' => 'module_estab/message_js',
			'wrapper' => 'target',
			'method' => 'replace',
			'effect' => 'fade',
		),
	);
	
	$form['e_details']['target'] = array(
		'#type' => 'markup',
		'#prefix' => "<div id='target'>",
		'#value' => 'No Country Selected',
		'#suffix' => '</div>',
	);
	
	$form['e_details']['target1'] = array(
		'#type' => 'markup',
		'#prefix' => "<div id='target1'>",
		'#value' => 'No Province Selected',
		'#suffix' => '</div>',
	);

So with the above, I select the country it creates the select box with the title "You selected that because..." but when I select a value in the "You selected that because..." select box it never gives me the next select box which has the title "You selecRRRse...".

Anyone know why and how I need to get this to work. This is the very last part of my module and then it is complete. Been struggling with this code for the last month and have no idea who I can speak to to get this to work :((

Please help.

Thanks in advance,
Davin

Comments

Trappies’s picture

Any ideas guys?