I am creating a custom module. And I'm having trouble with ajax. I've looked for the answer haven't found anyone with a similar problem.
What's supposed to happen:
Insert a date on the textfield. Click the button and the data below will change depending on the date. Data is stored on json files.
What's the problem:
Only the first request works. From the second request on it gives an error code 200 with an empty response.
What I've tested
I have tested with a simpler ajax where I get a specific data from a file and output the data on a div. It stops working when I use the "page" element.
Here's my code:
$form['dates'] = array(
'#type'=>'textfield',
'#title'=>'Choose the date',
'#id'=>'datepicker',
'#default_value'=>$selected,
);
$form ['submit'] = array (
'#type' => 'submit',
'#ajax' => array (
'callback' => 'freports_alunos_dados_callback',
'wrapper' => 'dados_content_wrapper',
'effect' =>'fade',
)
,
'#value' => t ( 'Ver Dados' )
);
$form['page'] = array(
'#theme' => 'freports_alunos_tpl',
'#type' => 'markup',
'#prefix' => '<div id="dados_content_wrapper">',
'#suffix' =>'</div>',
'#dados' => freports_get_dados($selected),
);
function freports_alunos_dados_callback($form, &$form_state){
$element = $form['page']; // << FROM MY TESTS THE PROBLEM STARTS HERE
$element['#dados'] = freports_get_dados($form_state['values']['datas']);
return $element;
}
function freports_theme($existing, $type, $theme, $path) {
return array (
'freports_alunos_tpl' => array (
'variables' => array (
'dados' => NULL
),
'template' => 'tpl/alunos'
),
'freports_curso_alunos_tpl' => array (
'variables' => array (
'dados' => NULL,
'filial' => NULL,
'curso' => NULL
),
'template' => 'tpl/curso_alunos'
),
'freports_turma_alunos_tpl' => array (
'variables' => array (
'dados' => NULL,
'filial' => NULL,
'curso' => NULL,
'turma' => NULL
),
'template' => 'tpl/turma_alunos'
)
);
}
Page TPL:
<?php
drupal_add_js(drupal_get_path('module','freports').'/js/freports.js','file');
?>
<div>
<!-- <strong>Escolher Data: </strong><input type="text" id="datepicker"><button onclick="getData(document.getElementById('datepicker').value)">Go</button><br /> -->
<strong>Atualizado em: </strong><?php print $dados->updated ?><br /><br />
<strong>Total Alunos: </strong><?php print $dados->totalAlunos;?><br />
<strong>Total Gama: </strong><?php print $dados->filial[0]->totalAlunos; ?><br />
<strong>Total Lago: </strong><?php print $dados->filial[1]->totalAlunos; ?><br />
<br /><br />
<table>
<tr ALIGN=center>
<td COLSPAN="2"><strong> CAMPUS - GAMA </strong></td>
</tr>
<tr>
<td><strong>Curso</strong></td>
<td><strong>Total Alunos<strong></td>
</tr>
<?php $i=0; foreach($dados->filial[0]->cursos as $cursos_gama){ ?>
<tr style="border-bottom:1px solid black;">
<td><a href="<?php print 'alunos/filial/0/curso/'.$i ?>"><?php print $cursos_gama->nome; ?></a></td>
<td style="text-align:right;"><?php print $cursos_gama->totalAlunos; ?></td>
</tr>
<?php $i++;} ?>
</table>
<br /><br />
<table>
<tr ALIGN=center>
<td COLSPAN="2"><strong> CAMPUS - LAGO </strong></td>
</tr>
<tr>
<td><strong>Curso</strong></td>
<td><strong>Total Alunos<strong></td>
</tr>
<?php $i=0; foreach($dados->filial[1]->cursos as $cursos_lago){ ?>
<tr style="border-bottom:1px solid black;">
<td><a href="<?php print 'alunos/filial/1/curso/'.$i ?>"><?php print $cursos_lago->nome; ?></a></td>
<td style="text-align:right;"><?php print $cursos_lago->totalAlunos; ?></td>
</tr>
<?php $i++;} ?>
</table>