I have 2 set of fields: Set A (Field1, Field2), and Set B (Field3, Field4).
- Field1 loads data from Mariadb tableA according to user selection and then loads the corresponding data from tableA to Field2. In order for this to happen, a .php file named 1-2.php is used. Field1 and Field2 work ok but:
Issue for Set A: Inspect Element->Console in the Front End shows all data of the Field2.
- Field3 loads data from Mariadb tableB according to user selection and then loads the corresponding data from tableB to Field4. In order for this to happen, a .php file named 3-4.php is used. Field3 loads ok from tableB but:
Issue for Set B: Field4 shows nothing!
*Note tableA and tableB structure is identical
1. Set A (Field1 and Field2)
Field1->Actionscript->Custom->Change:
function ff_field1_action(element, action)
{
switch (action) {
case 'change':
jQuery.ajax({
type: "POST",
url: "http://someurl/1-2.php",
data: { field1: element.value },
success: function(data) {
var obj = JSON.parse(data);
jQuery('#ff_elem2496').empty();
var arr = obj;
jQuery('#ff_elem2496').append(jQuery('<option>', {
value: '',
text: 'whatever'
}));
for (var i = 0; i < arr.length; i++) {
console.log(arr[i]);
jQuery('#ff_elem2496').append(jQuery('<option>', {
value: arr[i],
text: arr[i]
}));
}
}
});
break;
default:;
} // switch
} // ff_field1_action
Note: elem2496 refers to Field2
1-2.php file for Set A:
<?php
define('_JEXEC', 1);
if (file_exists(__DIR__ . '/defines.php'))
{
include_once __DIR__ . '/defines.php';
}
if (!defined('_JDEFINES'))
{
define('JPATH_BASE', __DIR__);
require_once JPATH_BASE . '/includes/defines.php';
}
require_once JPATH_BASE . '/includes/framework.php';
$judgePick1 = JRequest::getVar(field1);
$db1 = JFactory::getDbo();
$query1 = $db1->getQuery(true);
$query1
->select('x.name')
->from($db1->quoteName('#__tableA', 'x'))
->join('LEFT', $db1->quoteName('#__tableA', 'y') . ' ON ' . $db1->quoteName('x.parent') .' = '. $db1->quoteName('y.id'))
->where($db1->quoteName('y.name') .' = '. $db1->quote($judgePick1))
->andWhere(array($db1->quoteName('x.parent').' = '. $db1->quoteName('y.id'), $db1->quoteName('x.active').' = 1'), $glue = 'AND')
->order($db1->quoteName('x.name') . ' ASC');
$db1->setQuery($query1);
$result1 = $db1->loadColumn();
if(!$result1){
echo "error";
} else {
echo json_encode($result1);
}
?>
2. Set B (Field3 and Field4)
Field3->Actionscript->Custom->Change:
function ff_field3_action(element, action)
{
switch (action) {
case 'change':
jQuery.ajax({
type: "POST",
url: "http://someurl/3-4.php",
data: { field3: element.value },
success: function(data) {
var obj = JSON.parse(data);
jQuery('#ff_elem3871').empty();
var arr = obj;
jQuery('#ff_elem3871').append(jQuery('<option>', {
value: '',
text: 'whatever2'
}));
for (var i = 0; i < arr.length; i++) {
console.log(arr[i]);
jQuery('#ff_elem3871').append(jQuery('<option>', {
value: arr[i],
text: arr[i]
}));
}
}
});
break;
default:;
} // switch
} // ff_field3_action
Note: elem3871 refers to Field4
3-4.php file for Set B:
<?php
define('_JEXEC', 1);
if (file_exists(__DIR__ . '/defines.php'))
{
include_once __DIR__ . '/defines.php';
}
if (!defined('_JDEFINES'))
{
define('JPATH_BASE', __DIR__);
require_once JPATH_BASE . '/includes/defines.php';
}
require_once JPATH_BASE . '/includes/framework.php';
$judgePick2 = JRequest::getVar(field3);
$db2 = JFactory::getDbo();
$query2 = $db2->getQuery(true);
$query2
->select('x.name')
->from($db2->quoteName('#__tableB', 'x'))
->join('LEFT', $db2->quoteName('#__tableB', 'y') . ' ON ' . $db2->quoteName('x.parent') .' = '. $db2->quoteName('y.id'))
->where($db2->quoteName('y.name') .' = '. $db2->quote($judgePick2))
->andWhere(array($db2->quoteName('x.parent').' = '. $db2->quoteName('y.id'), $db2->quoteName('x.active').' = 1'), $glue = 'AND')
->order($db2->quoteName('x.name') . ' ASC');
$db2->setQuery($query2);
$result2 = $db2->loadColumn();
if(!$result2){
echo "error";
} else {
echo json_encode($result2);
}
?>
Finally, in Form->Advanced->More Options->Form Pieces->Before Form:
// Set A
$this->execPieceByName('ff_InitLib'); //Include BreezingForms Library
$db1 = JFactory::getDBO();//Get Database Object
$db1->setQuery('SELECT name FROM #__tableA WHERE parent=0 AND active=1 ORDER BY id ASC');
$result1 = $db1->loadColumn(); //load the result from the query
$res1 = "0;whatever;0\n";
for ($i = 0; $i < count($result1); $i++)
{
$res1 .= "0;".$result1[$i].";".$result1[$i]."\n";
}
function ff_setSelectList1($name, $value)
{
global $ff_processor;
for ($r = 0; $r < $ff_processor->rowcount; $r++)
{
$row =& $ff_processor->rows[$r];
if ($row->name==$name)
$row->data2 = $value;
unset($row);
} // for
} // ff_setSelectList1
ff_setSelectList1('field1', $res1);
// Set B
$this->execPieceByName('ff_InitLib'); //Include BreezingForms Library
$db2 = JFactory::getDBO();//Get Database Object
$db2->setQuery('SELECT name FROM #__tableB WHERE parent=0 AND active=1 ORDER BY id ASC');
$result2 = $db2->loadColumn(); //load the result from the query
$res2 = "0;whatever2;0\n";
for ($i = 0; $i < count($result2); $i++)
{
$res2 .= "0;".$result2[$i].";".$result2[$i]."\n";
}
function ff_setSelectList2($name, $value)
{
global $ff_processor;
for ($r = 0; $r < $ff_processor->rowcount; $r++)
{
$row =& $ff_processor->rows[$r];
if ($row->name==$name)
$row->data2 = $value;
unset($row);
} // for
} // ff_setSelectList2
ff_setSelectList2('field3', $res2);
The structure of Table A and Table B is the following:
Id parent name uri active
1 0 greekname1 latinname1 1
2 0 greekname2 latinname2 1
3 0 greekname3 latinname3 1
4 1 greekname4 latinname4 1
5 1 greekname5 latinname5 1
6 2 greekname6 latinname6 1
7 3 greekname7 latinname7 1
if ($row->name==$name) $row->data2 = $value; unset($row);
You see, you have not written curly braces so the$row->data2
declaration is conditional, but theunset($row)
is unconditionally executed. This looks odd to me (in two places). Is this the cause of your trouble? If so, I can post an answer. – mickmackusa♦ Apr 19 '20 at 21:42JRequest::getVar(field1);
andJRequest::getVar(field3);
these attempts to grab submitted values are using an undefined CONSTANT calledfield#
, but you want to access the values relating to the string keys, so you must wrap those words in single or double quotes. Actually, these are not fatal, they will merely generate warnings. 3v4l.org/QgJ0L so this is not a resolving fix. I'm afraid I am going to need to know the incoming value and your database schema. – mickmackusa♦ Apr 20 '20 at 7:09