Forums

Dynamic checkbox group with Ajax

ieqg 01 Oct, 2014
hello,

First sorry for my english, I'm french ...

I have a form with different dynamic drop down lists populated by Ajax events. It works perfectly by following this link:
http://www.chronoengine.com/faqs/70-cfv5/5232-how-do-i-build-a-dynamic-drop-down-in-cfv5.html

I'd like to do the same thing with a group of check boxes but I do not.

I tried without success:
http://www.chronoengine.com/faqs/58-cfv4/cfv4-elements-and-html/2647-how-do-i-build-a-select-drop-down-radio-button-or-checkbox-group.html

The DB Read action works properly.
I think the problem comes from the Custom code action.

Can you help me?
ieqg 02 Oct, 2014
When I use :

<?php
$results = array();
$results[] = '=??';
foreach ( $form->data['articles'] as $v ) {
  $results[] = $v['id'].'='.$v['title'];
}
$results = implode("\n", $results);
echo $results;
$mainframe =& JFactory::getApplication();
$mainframe->close();
?>


I have this error :

=??
QGBDJ=BA DUAN JIN
QGEM1=ETIREMENTS MERIDIENS 1
QGEM2=ETIREMENTS MERIDIENS 2
QGANA=ANATOMIE
QGWD1=WU DANG 1
QGEVAL=Evaluations et certification<br />
<b>Strict Standards</b>:  Only variables should be assigned by reference in <b>/xxx/xxx/www/administrator/components/com_chronoforms5/chronoforms/actions/custom_code/custom_code.php(20) : eval()'d code</b> on line <b>9</b><br />


Any idea ?
ieqg 02 Oct, 2014
When I use this Code custom :
<?php
$options = array();
if ( !$form->data['Data'] || count($form->data['Data']) < 1 ) {
  // no result was found
  $options[] = 'Please select a category';
} else {
  foreach ( $form->data['Data'] as $d ) {
    $options[$d['id']] =  $d['title'];
  }
}
echo json_encode($options);
?>


I have not error, but nothing happens.
My Ajax retour :
{"QGBDJ":"BA DUAN JIN","QGEM1":"ETIREMENTS MERIDIENS 1","QGEM2":"ETIREMENTS MERIDIENS 2","QGANA":"ANATOMIE","QGWD1":"WU DANG 1","QGEVAL":"Evaluations et certification"}
GreyHead 02 Oct, 2014
HI iegg,

Please remove the & from this line.
$mainframe =& JFactory::getApplication();
though you probably don't need Strict Standards reporting turned on as it may show up all kinds of 'Warning' messages.

Bob
ieqg 02 Oct, 2014
Hi,

Thanks GreyHead, I just tried and the error disappears (It also disappears when configuring Error_reporting).
But still nothing appears in my checkbox group 😟

Ajax return :
=??
QGBDJ=BA DUAN JIN
QGEM1=ETIREMENTS MERIDIENS 1
QGEM2=ETIREMENTS MERIDIENS 2
QGANA=ANATOMIE
QGWD1=WU DANG 1
QGEVAL=Evaluations et certification
GreyHead 02 Oct, 2014
HI iegg,

Sorry, I missed part of your question. The code you are using is written to add options to a drop-down list, not to create a new checkbox group. It's probably possible to do that but I have never tried - nor can I find any good examples from a quick Google search :-(

Is there some reason why a drop-down list will not work here?

Bob
ieqg 02 Oct, 2014
That's what I was afraid of.

Yet it seemed possible reading:
http://www.chronoengine.com/faqs/58-cfv4/cfv4-elements-and-html/2647-how-do-i-build-a-select-drop-down-radio-button-or-checkbox-group.html

These three form elements: select drop-downs, checkbox groups and radio button groups all accept lists of options i.e. value+label pairs. This FAQ shows you several ways in which ChronoForms can create option lists. Most of the FAQ will look at select drop-downs but the same methods can be used with checkbox groups or radio button groups.



I can't use drop-down list here because it has multiple options and pre checked according to previously selected option boxes.

Otherwise is it that there is another way to populate a checkbox group ?

Thank you in advance.
ieqg 03 Oct, 2014
Hi,

I found another solution by finding ideas in the forum.

I create my own event in Ajax :
<script type="text/javascript">
jQuery(document).ready(function($){
 $("#cursus").change(

  function() {

   cursus=$("#cursus option:selected").val();
   $.getJSON(
    "getStages.php",
    {"cursus":cursus}, //GET parameter

    function(data){
     $.each(data,function(code_stage,model){
      $("#fin-inscriptions").before("<input type='checkbox' value='"+code_stage+"' id='inscriptions["+model.id_stage+"]' />"+model.lib_stage+"<br />");
     }
    );}

   ); //end getJSON
  } //end ajax
 ); //end change event
}); //end ready function
</script>


and PHP code getStages.php:
<?php
    define( '_JEXEC', 1 );
    define( 'JPATH_BASE', realpath(dirname(__FILE__).'/' ));
    require_once ( JPATH_BASE .'/includes/defines.php' );
    require_once ( JPATH_BASE .'/includes/framework.php' );

    $mainframe = JFactory::getApplication('site');

function getStages($cursus){
 $db = JFactory::getDBO();
 $sql= "SELECT * FROM ins_stages WHERE id_cursus=".$cursus;
 $db->setQuery($sql);
 $rows = $db->loadObjectList();

 $models = array();
 foreach($rows as $row){
  $models[$row->code_stage]=$row;
 }
 return $models;
}
echo json_encode(getStages($_GET['cursus']));
?>


Now my check boxes are displayed correctly 😀
Only these data do not appear in $form->data 😟

How can I add?
Do you have any suggestions?
ieqg 03 Oct, 2014
As for me I forgot to put the "name" parameter in the input.

$("#fin-inscriptions").before("<input type='checkbox' name='inscriptions[]' value='"+code_stage+"' id='inscriptions"+model.id_stage+"' />"+model.lib_stage+"<br />");
This topic is locked and no more replies can be posted.