Hi,
I've been working for sometime on a module that seeks to use AJAX and jQuery to update some form fields before the submission of the form.
There is a table of diagnosis that has two columns, the diagnosis name and its corresponding code.The module seeks to take the diagnosis value and based on that, automatically populate the code field.
My implementation is as follows:
1 <?php
2 function posh_help($path,$args=NULL){
3 $output="";
4 switch($path){
5 case 'admin/modules#description':
6 $output= "Tests Carl's first form module";
7 break;
8 }
9 return $output;
10 }
11
12 function posh_perm(){
13 return array('access form','get help');
14 }
15 function posh_menu(){
16 $items=array();
17 $items['try']=array(
18 'title'=>t('Polarsky'),
19 'page callback'=>'drupal_get_form',
20 'page arguments'=>array('_posh_test_form'),
21 'access arguments'=>array('access form'),
22 'type'=>MENU_NORMAL_ITEM,
23 );
24 $items['try/%']=array(
25 'page callback'=>'_posh_test_form_ajax',
26 'page arguments'=>array(1),
27 'access arguments'=>array('access form'),
28 'type'=>MENU_CALLBACK
29 );
30 $items['help']=array(
31 'page callback'=> posh_help,
32 'access arguments'=>array('get help'),
33 );
34 return $items;
35 }
36
37 function _posh_test_form(){
38 $path=drupal_get_path('module','posh');
39 drupal_add_js($path.'/posh.js');
40 $form=array();
41 $form['name']=array(
42 '#type'=>'textfield',
43 '#prefix'=>'<table><tr><td>',
44 '#suffix'=>'</td>',
45 '#required'=> TRUE,
46 );
47 $form['code']=array(
48 '#type'=>'textfield',
49 '#prefix'=>'<td>',
50 '#suffix'=>'</td></tr></table>',
51 '#required'=>TRUE,
52 );
53 $form['submit']=array(
54 '#type'=>'submit',
55 '#value'=>t('Submit'),
56 );
57 return $form;
58 }
59 function _posh_test_form_submit($form,$form_state){
60 $message=t('Your form has been submitted successfully');
61 drupal_set_message($message);
62 }
63 function _posh_test_form_ajax(){
64 if ($_SERVER['HTTP_X_REQUESTED_WITH'] !== 'XMLHttpRequest'){
65 return MENU_ACCESS_DENIED;
66 }
67 $diagnosis=$_POST['name'];
68 $sql="SELECT * FROM {diagnosis} WHERE name=(%s)";
69 $result=db_query($sql,$diagnosis);
70 $final=db_fetch_object($result);
71 return drupal_json(array('code'=>$final->code));
72 exit;
73 }
My jQuery code is as follows;
1 Drupal.behaviours.posh = function(context){
2 $('#edit-name',context).bind('keyup', function(){
3 var diagnosis=$('#edit-name').val();
4 $.post('try/%',diagnosis,myway);
5 return false;
6 });
7 }
8 var myway= function(data){
9 var result= Drupal.parseJson(data);
10 $('div #edit-code').html(result['code']);
11 }
Now what I don't get is this.When I enter a value in the diagnosis field, nothing happens.I expected the code field to get updated with the corresponding code of the particular diagnosis. What could be wrong, please....?
PS:I'm really desperate to get around this and would be grateful for any help.
Comments
This looks more like a jquery
This looks more like a jquery issue than a Drupal issue.
But I'd say part of your problem is this: $.post('try/%',diagnosis,myway);
try/% is not a valid URL.
Contact me to contract me for D7 -> D10/11 migrations.
Hola Jay, Thanks a lot for
Hola Jay,
Thanks a lot for your suggestion:it actually helped me a lot.I've been able to get the module to do what I wanted......should probably get it posted to help others who might be facing the same problem.
Cheers,
Polarsky.