hi,
I'm new to drupal and have already fallen in love with it. I am currently working on a module that requires the use of forms and got this issue. I want my form to be able to use the value of unsubmitted form fields. how do I accomplish this? an example is below:
There is a table of various diagnoses with related information.The form below seeks to accept the diagnosis and complete the other form fields based on the value of the 'diagnosis' field.

function myform(){
//take diagnosis
$form['diagnosis']=array(
'#type'=>'textfield',
'#required'=>TRUE);

//use unsubmitted diagnosis field value to perform a query

if ($diagnosis=isset($form['diagnosis'])?$form['diagnosis']:''){
$q="SELECT * FROM {diagnosis} WHERE name=%s";
$result=db_query($q,$diagnosis);
$final= db_fetch_object($result);
}
// assume every diagnosis has a designated code

$form['code']=array(
'#type'='textfield',
'#required'=>TRUE,
//complete this field with 'code field' in the {diagnosis} table(from the query above)
'#default_value'=>$final->code);
}
//submit function comes here
function myform_submit()........

Is this going to work?I need help

Comments

vasi1186’s picture

In order to use unsubmitted form fields, actually you have to pass these values to the form constructor function, because you have to pre-populate the fields. For example:

function myform($form_state=array(), $diagnosis){
$form = array();
if ($diagnosis){
//take diagnosis
$form['diagnosis']=array(
'#type'=>'textfield',
'#default_value' => $diagnosis
'#required'=>TRUE);
$q="SELECT * FROM {diagnosis} WHERE name=%s";
$result=db_query($q,$diagnosis);
$final= db_fetch_object($result);

// assume every diagnosis has a designated code

$form['code']=array(
'#type'='textfield',
'#required'=>TRUE,
//complete this field with 'code field' in the {diagnosis} table(from the query above)
'#default_value'=>$final->code);
}
return $form;
}

Now, another thing is that because the diagnoses is a textfield, the user could change its value, and this not makes sense... So, you could just use a simple markup element just to show the diagnosis to the user and keep the diagnosis in a hidden field (or a value field), like that:

$form['diagnosis_view']=array(
'#value'=>'The diagnoses: '.$diagnosis
);
$form['diagnosis']=array(
'#type'=>'value',
'#value' => $diagnosis
);

I hope I understood well what are your needs and my explanation will help you.

Vasi.

polarsky’s picture

Thanks so much Vasi for your response,its been very helpful.
However, there is a developing issue I need help with.I implemented your suggestion in the module as follows:

 <?php
    /*Implement hooks for module
     *
    */   
    function polar_help($path,$arg=NULL){
    $output = "";
    switch($path){
    case 'admin/modules#description':
    $output = "t('Implements my first form.')";
    break;
    }
    return $output;
    }
     
    function polar_perm(){
    $perms= array(
    'checkout form','get help');
    return $perms;
     }
     
    function polar_menu(){
    $items['check']=array(
    'title'=>t('Polarsky'),
    'page callback'=>'myform',
    'access arguments'=>array('checkout form'),
    'type'=>MENU_NORMAL_ITEM,
      );
     $items['trial']=array(
    'page callback'=>'polar_help',
    'access arguments'=>array('get help'),
    'type'=>MENU_NORMAL_ITEM,
     );
     return $items;
     }
      
   function myform($form_state=array(), $diagnosis){
   $form = array();
    if ($diagnosis){
    //take diagnosis
    $form['diagnosis']=array(
    '#type'=>'textfield',
    '#default_value' => $diagnosis,
    '#required'=>TRUE);
    $q="SELECT * FROM {diagnosis} WHERE name=%s";
    $result=db_query($q,$diagnosis);
    $final= db_fetch_object($result);
     
    // assume every diagnosis has a designated code
    
    $form['code']=array(
    '#type'=>'textfield',
    '#required'=>TRUE,
    //complete this field with 'code field' in the {diagnosis} table(from the query above)
    '#default_value'=>$final->code);
    $form['submit']=array(
    '#type'=>'submit',
    '#value'=>t('Submit'),
     );
     }
    return $form;
    }
    function myform_submit($form,&$form_state){
    $q="INSERT INTO {diagnosis} (name,code) VALUES (%s,%s)";
    $r=db_query($q,$form_state['values']['diagnosis'],$form_state['values'][        'code']);
}

Currently, when I click on the 'Polarsky link in the menu, i get a page with this:

"warning: Missing argument 2 for myform() in /var/www/drupal6/modules/polarsky/polarsky.module on line 39."

What could be the cause?I know this is not exactly the same issue I filed yesterday but i would be glad if u could give me a hand.
In real-time I want my form to work this way: that as soon as a user enters a value in the diagnosis field(without submitting the form), the code field becomes populated with the corresponding 'code' value of that particular diagnosis from the database.Is this the right approach to achieving that........?
PS: the diagnosis table looks like this:
+---------+------+
| name | code |
+---------+------+
| malaria | 437 |
| cold | 609 |
+---------+------+

vasi1186’s picture

For the first issue, with the warning, this appears because your form constructor (the "myform" function) expects the second parameter, that is $diagnosis. To remove this warning, just assign a default value for $diagnosis, for the cases when the function is called without this parameter. So, the new function header would be:
function myform($form_state=array(), $diagnosis = "").

For the second problem, you can resolve it with AJAX. Here would be the steps:
1) Define a new entry in the menu, something like this:
function polar_menu(){
...........
$items['check/%']=array(
'page callback'=>'polar_search_code',
'page arguments' => array(1),
'access arguments'=>array('checkout form'),
'type'=>MENU_CALLBACK,
);
return $items;
}

2) Implement your search function:
function polar_search_code($keys){
//check first the the request is through AJAX, else just return the MENU_ACCESS_DENIED.
if ($_SERVER['HTTP_X_REQUESTED_WITH'] !== 'XMLHttpRequest'){
return MENU_ACCESS_DENIED;
}
//Here would be your search logic, and the return value will be a string, representing the code, or an empty string if no code was found.
......
//for this kind of requests, you just need the search result, so there is no need to go into the whole process of templating, theming, etc...
module_invoke_all('exit');
print $search_result_string;
die();
}

3) Implement your javascript. I recommend to use jQuery. With jQuery, making AJAX requests is very very easy to implement.

The javascript should catch the "keyup" event, get the value entered in the textfield, make the Ajax request, get the response, and put the response in the desired textfield.

Also in drupal 6, you have the AHAH functionaliy, that is implemented in core, maybe you also could take a look on it: http://drupal.org/node/348475

Vasi.

polarsky’s picture

hola,
I tried an implementation of the ajax-jQuery thing but I'm not sure if it is the correct implementation.I'd be very grateful if you good people could give me a hand.
currently, my code looks like this:

 1 <?php
      2 /*Implement hooks for module
      3  *
      4  */
      5 function polarsky_help($path,$arg=NULL){
      6 $output = "";
      7 switch($path){
      8 case 'admin/modules#description':
      9 $output = "t('Implements polarsky's first form.')";
     10 break;
     11 }
     12 return $output;
     13 }
     14
     15 function polarsky_perm(){
     16 $perms= array(
     17 'checkout form','get help');
     18 return $perms;
     19 }
     20
     21 function polarsky_menu(){
     22 $items['check']=array(
     23 'title'=>t('Polarsky'),
24 'page callback'=>drupal_get_form('myform'),
     25 'access arguments'=>array('checkout form'),
     26 'type'=>MENU_NORMAL_ITEM,
     27 );
     28 $items['check/%']=array(
     29 'page callback'=>'polarsky_search_code',
     30 'page arguments' => array(1),
     31 'access arguments'=>array('checkout form'),
     32 'type'=>MENU_CALLBACK,
     33 );
     34 $items['trial']=array(
     35 'title'=>t('Help'),
     36 'page callback'=>'polarsky_help',
     37 'access arguments'=>array('get help'),
     38 'type'=>MENU_NORMAL_ITEM,
     39 );
     40 return $items;
     41 }
     42
     43
     44
     45
     46 function myform($form_state=array(), $diagnosis=""){
47 $form = array();
     48 if ($diagnosis){
     49 //take diagnosis
     50 $form['diagnosis']=array(
     51 '#type'=>'textfield',
     52 '#default_value' =>$diagnosis,
     53 '#required'=>TRUE);
     54 $q="SELECT * FROM {diagnosis} WHERE name=%s";
     55 $result=db_query($q,$diagnosis);
     56 $final= db_fetch_object($result);
     57
     58 // assume every diagnosis has a designated code
     59
     60 $form['code']=array(
     61 '#type'=>'textfield',
     62 '#required'=>TRUE,
     63 //complete this field with 'code field' in the {diagnosis} table(from th        e query above)
     64 '#default_value'=>$rep);
     65 $form['submit']=array(
     66 '#type'=>'submit',
     67 '#value'=>t('Submit'),
     68 );
69 }
     70 return $form;
     71 }
     72 function myform_submit($form,&$form_state){
     73 $q="INSERT INTO {diagnosis} (name,code) VALUES (%s,%s)";
     74 $r=db_query($q,$form_state['values']['diagnosis'],$form_state['values'][        'code']);
     75 }
     76 function polarsky_search_code($keys){
     77 //Check if request is through ajax or return MENU_ACCESS_DENIED
     78
     79 if ($_SERVER['HTTP_X_REQUESTED_WITH'] !== 'XMLHttpRequest'){
     80 return MENU_ACCESS_DENIED;
     81 }
     82 $q="SELECT * FROM {diagnosis} WHERE name=%s";
     83 $result=db_query($q,$diagnosis);
     84 $final= db_fetch_object($result);
     85 $rep=$final->code;
     86 module_invoke_all('exit');
     87 print $search_result_string;
     88 die();
     89 }
//the corresponding javascript code (using jQuery)
    
     90 function jscode{
     91 $(document).ready(function(){
     92 $("form#myform").keyup(function(event){
     93 $.ajax({
     94         type: "POST",
     95         url: "polarsky_search_code",
     96          });
     97        });
     98  });
     99}

PS: I am very confused about how to do this correctly..is this the right approach..?I mean the ajax-javascript thing? and is the jQuery code supposed to be in a new file? how do I get the form field whose value i want to capture with the javascript 'keup' event(line 92)?I'd be grateful if you could point any bug in the code to me...
merci!

summit’s picture

Subscribing, greetings, Martijn