Hi All,
This the scenario i am trying to emulate:
1.User Lands on Page
2. User selects a source language from dropdown box : For eg 'en-us'
3. The list box benath the source language gets populated with values of target languages for 'en-us'

This is the screen i have in Drupal 7. Image 1
The code overview:

On selection source language the script.js gets called. The javascript makes a call to another php named
webDelegate.php which provides the list of target languages using json.

script.js

(function($){

$(document).ready(function() {
  $("#edit-folks-list").change(function() {
    var targetLangIndex = $(this).find(":selected").text();
    var chunks = targetLangIndex.split("_");
    var idChunk = chunks[1];
    $('#edit-targetLang-list').empty();
	var currUser = $('#hidden_id').val();
	
	 $('#edit-webdelegate-list').empty();
	
	
     $.getJSON('/drupal7cms/translate/webDelegate.php?name='+idChunk+'&curentUser='+currUser, function(data) {
	    $.each(data, function(key, value) {  			
			$('#edit-webdelegate-list').append($('<option>', { key : value }).text(value));
		});
		
		
		
    });
  });
}); 

})(jQuery); 


   (function($){
   
$(document).ready(function () {

  $("#formModifications-translate-bulk").submit(function() {
    $(":submit", this).attr("disabled", "disabled");
	
  });
  
});
})(jQuery); 


webDelegate.php

// Database connection.
mysql_connect("localhost", "root", "admin");
mysql_select_db("drupal7cms");

$curuname = trim($_GET['curentUser']);



$resultFindMode = mysql_query("SELECT mode_id FROM freeway_mode where sr_id = 1");

while ($recordUsPw = mysql_fetch_object($resultFindMode ))
		 {
			$modeValueUsPw = $recordUsPw->mode_id;		
		 }
		 
	if ($modeValueUsPw == '1'){
//Obtain credentials
		}

		}	
		
		if ($modeValueUsPw == '2'){

		//Obtain credentials
		 }
		
		}
		 
$myid = trim($_GET['name']);

$arrayWebD = array();
$LoginClient = new SoapClient("https://wsdl); 
$ServicesLink = new SoapClient("https://wsdl);

	  try{	  
	  $arrResponse = $LoginClient->Logon(array ('Username'=>$fusername,'Password'=>$fpassword));
	  $ticket = ($arrResponse->LogonResult);
	  $getTarLang = $ServicesLink->GetTargetLanguages (array('Ticket'=>$ticket,'SourceLanguageID'=>$myid));
	  					
		//Code for obtaining langs
	  }
	  catch (SoapFault $exception){
		  return $exception;
		  }
		  
		  
print json_encode($arrayWebD);
 

Now this retured list will be populated in the listbox.This is the form i was using.

$arrayWebD = array();
 
 
    $form['webDelegate_list'] = array(
    '#type' => 'select',
    '#title' => t('Freeway Target Languages'),
    '#options' => $arrayWebD,
    '#multiple' => true,
    '#default_value' => $arrayWebD,
	'#value' => $arrayWebD,
    '#attributes' => array(
      'size' => 4,
      'class' => 'select_folks_list',
    ),
    '#weight' => 8,
  );
  

The above form and the code above it worked with out any error . However the selections of the list box were undetected
(Probably because we ae using $arrayWebD which we just declared above the form ? i am not sure ). I f i use the
value $arrayWebD = array("French (France)_fr-fr","Icelandic_is-IS"); for the #value field, i saw that the list box selections are all detected as French and Icelandic irrespective of what the user actually chose :(

If i create the form with out the value, i see this An Ilegal choice has been detected error.

How should I be adding the value to avoid the illegal choice error as well as to get the selected listbox
values registered

Would like to have your inputs on the same.

Thanks A

Comments

nevets’s picture

Is the form defined in a menu as part of menu callback (it should be)

And you need to use form API's approach to doing this. The developer examples module includes examples for using forms and ajax to update them.

angela.simmons’s picture

Thanks nevets
can we have something like using hidden fields of the listbox to populate the actual visible listbox. The question is can we have arrays in the form populated with such values

$arrayForValue = array ('$array obtained from that hidden field value....(.val..something like that)')
 
//and then in the visible listbox
 
$form['webDelegate_list'] = array(
    '#type' => 'select',
    '#title' => t('Freeway Target Languages'),
    '#options' => $arrayWebD,
    '#multiple' => true,
    '#default_value' => $arrayWebD,
<strong> '#value' => $arrayForValue,</strong> 
   '#attributes' => array(
      'size' => 4,
      'class' => 'select_folks_list',
    ),
    '#weight' => 8,
  );


Thanks
A

theamoeba’s picture

you cannot have a #value and a #default_value, better to just use #default_value. Also you cannot use an array as a default value, you must use a key from the option you want selected.


// Let's pretend that we have gotten some values from webdelegate.php
$arrayWebD = array(
  'en_us' => 'English US'
  'en_za' => 'English ZA',
);

// And then in the visible listbox
$form['webDelegate_list'] = array(
    '#type' => 'select',
    '#title' => t('Freeway Target Languages'),
    '#options' => $arrayWebD,
    '#multiple' => true,
    '#default_value' => 'en_za',
   '#attributes' => array(
      'size' => 4,
      'class' => 'select_folks_list',
    ),
    '#weight' => 8,
);