Forums

Disable a field when a certain amount of people have subscribed

goliath 19 May, 2015
I use this code to redirect visitors to another url when a certain amount of people have subscribed my form:


<?php
$db =& JFactory::getDBO();
$query = "
    SELECT COUNT(*)
        FROM `#__my_table` ;
";
$db->setQuery($query);
$count = $db->loadResult();
if ( $count >= 15 ) {
  // Redirect to my url
  $app =& JFactory::getApplication();
  $app->redirect('my_url');
}
?>


This works fine. But is there a possibility to disable a specified field (e.g. 'datum') in my form instead of redirecting to another url?

I found this code from Bob:


<?php
$db =& JFactory::getDBO();
$query = "
    SELECT COUNT
        FROM `#__some_table`
        WHERE `course_id` = 'xxx' ;
";
$db->setQuery($query);
$count = $db->loadResult();
$form->data['open'] = false;
If ( $count < 999 ) {
  $form->data['open'] = true;
}
?>


Then you can use $form->data['open'] to manage the form display.


But I don't understand on how to do so.
GreyHead 19 May, 2015
Hi geertmartens,

You could just add some JavaScript from a Load JS action:
jQuery(document).ready(function (jQ) {
<?php
if ( $form->data['open'] ) {
  echo "jQ('#date_id').prop('disabled', false);";
} else {
  echo "jQ('#date_id').prop('disabled', true);";
}
?>
});

Bob
goliath 19 May, 2015
This works Bob!
Is there also the possibility to change the value of 'date_id'?

E.g.:
Say that:
echo "jQ('#date_id').prop('disabled', true);";

results in the value: " 16/09/2015 "
And I want it to result in the value: " 16/09/2015 (Full) "

Is this possible?
GreyHead 19 May, 2015
Hi geertmartens,

You can do that back in the original code
. . .
if ( $count < 999 ) {
  $form->data['open'] = true;
} else {
  $form->data['date_id'] += ' (full)'; 
}
?>

Bob
goliath 19 May, 2015
Thanks Bob.

I used this code:


<?php
$db =& JFactory::getDBO();
$query = "
    SELECT SUM(`aantal`)
        FROM `my_table`
        WHERE `datum` = 'my_value' ;
";
$db->setQuery($query);
$count = $db->loadResult();
$form->data['open'] = false;
If ( $count <= 15 ) {
  $form->data['open'] = true;
}
?>


And replaced it with this:


<?php
$db =& JFactory::getDBO();
$query = "
    SELECT SUM(`aantal`)
        FROM `my_table`
        WHERE `datum` = 'my_value' ;
";
$db->setQuery($query);
$count = $db->loadResult();
$form->data['open'] = false;
If ( $count <= 15 ) {
  $form->data['open'] = true;
} else {
  $form->data['datum'] += ' (full)'; 
}
?>


But that results in the same value: " 16/09/2015 " and not " 16/09/2015 (full) ".

What am I doing wrong?
GreyHead 19 May, 2015
Hi geertmartens,

No idea - the code looks OK. Please try adding a Debugger after the Custom Code and see if the value of datum has actually changed.

Bob
goliath 20 May, 2015
Hi Bob

The only thing I can see in the debugger is:

Array
(
    [option] => com_chronoforms5
    [chronoform] => TESTform
    [com_twojtoolbox_filelist_css] => Array
        (
        )

    [com_twojtoolbox_filelist_js] => Array
        (
        )

    [com_twojtoolbox_filelist_less] => Array
        (
        )

    [open] => 1
    [datum] => 0
)
GreyHead 20 May, 2015
Hi geertmartens,

In that case [open] => 1 means that is true and so the value of datum should be unchanged? But it appears to be set to 0 now??

Bob
This topic is locked and no more replies can be posted.