Forums

help with json on double dropdown

teldrive 09 Jun, 2014
I apreciate if someone can help
I used the demo of CFv5 to apply it to my form, all is working fine except that keys are not considered as keys, I have debugged it with firebug but I couldn't find the right conbination on this concatenation "=>", ":", "=" ????
 ["Seleccione Hospital","18:Hospital San Agust\u00edn","19:Hospital Carmen\u00a0y Severo Ochoa","20:Hospital Comarcal de Jarrio","21:Hospital de Cabue\u00f1es","22:Hospital Cruz Roja Espa\u00f1ola de Gij\u00f3n","23:Hospital Valle del Nal\u00f3n","24:Hospital Universitario Central de Asturias","25:Hospital del Oriente de Asturias (Fundaci\u00f3n P\u00fablica Francisco Grande Covi\u00e1n)"]


<?php
$results = array();
$results[] = 'Seleccione Hospital';
foreach ( $form->data['hosp'] as $v ) {
 $results[] = $v['id'].':'.$v['hospital'];
}
echo json_encode($results);

[attachment=0]Captura.JPG[/attachment]
also I tried with Ajax tutorial code , but I couldn't make it working, get the response of Ajax with the array of fields but is not inserted in dropdown😲
<?php
$results = array();
$results[] = '=??';
foreach ( $form->data['hosp'] as $v ) {
  $results[] = $v['id'].'='.$v['hospital'];
}
$results = implode("\n", $results);
echo $results;
$mainframe =& JFactory::getApplication();
$mainframe->close();
?>
teldrive 10 Jun, 2014
Answer
1 Likes
I spent some time, but solved it
I have got some inspiration form
http://www.chronoengine.com/forums/posts/f2/t94009.html?hilit=json%20array&page=2

but so far it's simpler than this, Bob & Max has simplified it, best recommendation in June-2014 is download demo form called dynamic_dropdown
in events of fdropdown-1 must configure this, note that "hospital" is name of field of dropdown-2 that will recive data received from Json
[attachment=0]Captura.JPG[/attachment]
you have to define a new event in form-setup our case "load_ajax", include action to read db in our case with condition of selected field sent by event's dropdown1(this save a lot of JS code)
[attachment=1]Captura1.JPG[/attachment]
and finally json code that allow send array for dropdown2
[attachment=2]Captura2.JPG[/attachment]
the working code is
<?php
$results = array();
$results[] = 'Seleccione Hospital';
foreach ( $form->data['hosp'] as $v ) {
  $results[$v['id']] = $v['hospital'];
}
echo json_encode($results);
?>
This topic is locked and no more replies can be posted.