我有 2 组字段:Set A (Field1, Field2) 和 Set B (Field3, Field4)。
- Field1根据用户选择从Mariadb tableA中加载数据,然后将tableA中的相应数据加载到Field2中。为了实现这一点,使用了一个名为 1-2.php 的 .php 文件。Field1 和 Field2 工作正常,但:
Set A 的问题:Inspect Element->Console in Front End 显示 Field2 的所有数据。
- Field3根据用户选择从Mariadb tableB中加载数据,然后将tableB中的相应数据加载到Field4中。为此,使用了一个名为 3-4.php 的 .php 文件。Field3 从 tableB 加载正常,但是:
Set B 的问题:Field4 什么也没有显示!
*注意tableA和tableB的结构是一样的
1. 设置 A(Field1 和 Field2)
Field1->Actionscript->自定义->更改:
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
注意:elem2496 指的是 Field2
设置 A 的 1-2.php 文件:
<?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. 设置 B(Field3 和 Field4)
Field3->Actionscript->自定义->更改:
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
注:elem3871 指 Field4
Set B 的 3-4.php 文件:
<?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);
}
?>
最后在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);