I have researched the far ends of the internet and the inner depths of drupal.org. I am stumped!

I have a CCK content type with a select list field and a text box field.
I would like to set the default value of the text box field based on the value selected in the select list field by adding the appropriate php code to the “code” field under “Default Value” in the field configuration, which must return an array.

My initial though was ActiveSelect; but it only supports select list to select list associations and a module needs to be written from scratch. I then looked into jQuery.

My initial thought is to use jQuery to get the value of select list and then return this to Drupal in JSON format. I would then use this variable in my SQL string to generate the default value for the text field. I have found a Drupal to jQuery conversion function: drupal_to_js(). I have also found a Drupal function that de-serializes a JSON object: Drupal.parseJson(). But, I cannot find a jQuery function that serializes a string variable or data type to a JSON object.

As I understand it, when I call drupla_add_js(), I am executing in the JavaScript runtime and in order to pass data to the PHP runtime, I need to serialize my data via JSON. Correct? Pseudocode:

if(Drupal.jsEnabled){
drupal_add_js(

// get the select list value and store it
var = '$('#edit-field-date-0-value');'

// pseudo JSON conversion function
return toJSON(var);,

'inline'
);
}

I am still learning jQuery and Drupal development and have found “Pro Drupal Development” extremely helpful and “Learning jQuery” is in route to my house, but I could still really use your help.

My thought is that this approach would allow me to leverage my investment in the CCK content types that I have created without writing node modules from scratch. I am taking the wrong approach or this approach technically possible?

Than you in advance for your much appreciated feedback.

Take care.

Comments

nevets’s picture

The way I read this is you have a select list and when a user picks a value the text field is filled in based on that choice (what happens if the text field all ready has a value?) If this is correct for each option in the select list has a value and displayed value, which do you want placed in the text field?

SageOfCode’s picture

Nevets,

> "you have a select list and when a user picks a value the text field is filled in based on that choice"
correct.

>what happens if the text field all ready has a value?
The text field will be populated based on the select list value.
If text field already has a value, then it will be over-written with the new value based on the newly selected list value.

> "...each option in the select list has a value and displayed value, which do you want placed in the text field?"
The displayed value (i.e. the value that the user sees when he or she opens the drop down menu.)

Thank you for your prompt response.
I am looking forward to your additional feedback.

Cheers.

---------------------------
www.sageofcode.com

nevets’s picture

A starting point. The following example was tested with the latest copy of jQuery, it may need some tweeking if using the default version of jQuery that comes with Drupal 5. This code would code in the head section of your themes page.tpl.php file and needs to be modified to work with your setup.

<script type="text/javascript">
$(document).ready(
	function() {
		$('#edit-field-pick-one-key').change(
			function () {
				var str = "";
				$('#edit-field-pick-one-key option:selected').each(
					function () {
				  	str += $(this).text() + " ";
				  }
				);
		    $('#edit-field-another-choice-0-value').val(str);
			}
		).change();
	}
);
</script>

To modify the code you will need to replace 'pick-one' with the name of your selector field (replacing underscores (_) with dashes (-)). And then replacing 'another-choice' with the name of your text field (again replacing underscores with dashes).

SageOfCode’s picture

Very clever.

I will try this out.

Thanks again.

www.sageofcode.com

hmdnawaz’s picture

Your code did work for me.
But I have one extra question.
When i select an option in my select field I want to execute MySql query on the condition of jquery result.
For example i have a select field which contains a list of forum titles. I want when I select a forum title in the select field its posted date should be displayed in the text box not the title of the forum.

Please give me feedback.

Ahmad Nawaz
Acquia Certified Developer
Email: hmdnawaz@gmail.com
Skype: hmdnawaz

nevets’s picture

Small editorial comment, it is better to post new questions in their own thread.

To do that you will need some "extra" code, in general terms something with ajax that will fetch the date, details will depend on the version of Drupal your are using.