Closed (fixed)
Project:
Drupal core
Version:
5.1
Component:
forms system
Priority:
Minor
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
25 Jul 2007 at 14:43 UTC
Updated:
10 Aug 2007 at 11:00 UTC
Using the same concept as outlined brilliantly here http://blue.live4all.co.il/~mooffie/cms/node/15 (thanks Mooffie), I was trying to populate a CCK text field (Text | Text field) with information from database table based on user select (i.e. if a user selects Product ID, the product description text field is populated automatically) on an input form.
I found I was able to populating a Select list field (Text | Select List) field but I cannot seem to update the text field. Is this not possible with Drupal?
The PHP code I have been using in my allowed values section is as follows:
------------------------------
>
// Step 1: Find out the selected product.
global $_the_current_node;
if (isset($_the_current_node)) {
// Various scenarios of node editing:
if ($_POST) {
$product= $_POST['field_test_prod_num']['key'];
} else if (!empty($_the_current_node->nid)) {
// Editing an existing node. (e.g. q=node/123/edit)
$product= $_the_current_node->field_test_prod_num[0]['value'];
} else {
// Creating a new node. (e.g. q=node/add/product)
return array('' => t('- Select product -'));
}
} else {
// Viewing a node, or any other scenario:
return array();
}
//
// Step 2: Return the product belonging to the record.
//
$options = array('' => t(' Select product - '));
$res = db_query("SELECT DISTINCT field_product_name_value FROM {content_type_product} WHERE field_product_ID_value = '%s'", $product);
while ($o = db_fetch_object($res)) {
/* if (!$o->field_product_name_value) {
// If this deliverable has no name, the 'field_product_name_value' field
// might be empty. But since this is a required field we can't have an
// empty string here. So we use a dummy '-'.
$options['-'] = t('');
} else {*/
$options[$o->field_product_name_value] = $o->field_product_name_value;
/*}*/
}
return $options;
------------------------------
Please help.
Comments
Comment #1
robertdouglass commentedI think you've assigned this to the wrong project. Are you actually using the cck_taxonomy module?
Anyway, what you're missing is that Drupal has a very neat Forms API that you need to use if you plan on submitting data to Drupal.
http://drupal.org/node/37775
http://api.drupal.org/api/HEAD/file/developer/topics/forms_api.html
http://api.drupal.org/api/HEAD/file/developer/topics/forms_api_reference...
Check that out and you'll understand why your example won't work.
Comment #2
dantina commentedThanks and you are right, I am not using cck_taxonomy module - my apologies for the wrong assignment.
I am still having problems with this. I've gone through Form API reference pages - as much as I can and I am now using hook_form_alter to try to assign value to the field. I decided to test the function with by just assigning a text string to the field and that is not working either - the field is completely blank. The code I am using is as follows:
I am wondering if perhaps I am using the wrong name to identify the field, but I've used variations of the values for both the 'name' and 'id' attributes for the field and none worked! Here's the html code:
<input type="text" maxlength="25" name="field_test_prod_num[0][value]" id="edit-field_test_prod_num-0-value" size="60" value="" class="form-text" />Any idea what I am doing wrong? (And by the way - if you haven't guessed already - I am completely new to this. But as much as I was excited and impressed by drupal a few weeks ago, I feel completely frustrated right now).
Comment #3
robertdouglass commentedI'm not totally clear what you're trying to do... but if you have '#type' => 'value', you are telling the Form API that it should remember a value on the server for your form. This is an alternative to '#type' => 'hidden' which generates a hidden form element on the client. For your experimentation, try starting out with '#type' => 'textfield' and see if you are able to submit and access values with that.
Comment #4
dantina commentedThanks a lot Robert. Your suggestion worked and I now have a new understanding of #type!
I just have another small query. I've just set some fields to in the Display fields of a content type but I can still see the field on the input form. So, my question: How do you hide fields from users that you just want to store some values in for processing?
Comment #5
dantina commentedHi Robert, just noticed that the code only created a new field! I needed to update an existing field. The code I am using is this:
and it just created a new textfield with the new value. How do I change values in an existing field. I've tried this and it doesn't seem to work either:
Comment #6
robertdouglass commentedYou need to actually change the values in the $form that comes into hook_form_alter. I suggest you study existing examples from the codebase to see how this is done.
./modules/node/node.module:2480:function node_form_alter($form_id, &$form) {
./modules/taxonomy/taxonomy.module:669:function taxonomy_form_alter($form_id, &$form) {
To hide a field you can change its type to 'value'. Make sure its '#value' is set to the value you want it to keep.
Comment #7
dantina commentedThanks Robert - I'll give this a try.
Comment #8
dantina commented