Hi,
I'm trying to use ajax and Jquery in a custom form module and I need some help. The scenario is this:
There is a database that contains a table with two columns: a diagnosis and corresponding code.
Now my form module seeks to take the diagnosis and use ajax and jQuery to automatically populate the code field before the submit button is hit.
Presently, my code looks like this:
PS:I,m really confused about if this is the correct drupal implementation of ajax and jQuery. I badly need help.

 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 
     55 
     56 
     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?I'd be grateful if you could point any bug in the code to me...
merci!