$db =&JFactory::getDBO();
$inn_1 = 'SELECT * FROM `#__virtuemart_countries` WHERE virtuemart_country_id ="1"';
$kpp_1 = 'SELECT * FROM `#__virtuemart_countries` WHERE virtuemart_country_id ="1"';
$db->setQuery($inn_1);
$result = $db->loadObjectList();
foreach($result as $row){
$s1_i = $row->country_3_code;
$s1_k = $row->country_2_code; };
$inn_2 = 'SELECT * FROM `#__virtuemart_countries` WHERE virtuemart_country_id ="2"';
$kpp_2 = 'SELECT * FROM `#__virtuemart_countries` WHERE virtuemart_country_id ="2"';
$db->setQuery($inn_2);
$result = $db->loadObjectList();
foreach($result as $row){
$s2_i = $row->country_3_code;
$s2_k = $row->country_2_code;
};
...........
jQuery(function($) {
$('body').on('change', '#virtuemart_country_id', function() {
$ssnull = '';
$s1_i = '<?php echo $s1_i ?>'; $s1_k = '<?php echo $s1_k ?>';
$s2_i = '<?php echo $s2_i ?>'; $s2_k = '<?php echo $s2_k ?>';
....................
if($(this).val() == 0) {$('#inn_field').val($ssnull); $('#kpp_field').val($ssnull);}
if($(this).val() == 1) {$('#inn_field').val($s1_i); $('#kpp_field').val($s1_k);}
if($(this).val() == 2) {$('#inn_field').val($s2_i); $('#kpp_field').val($s2_k);}
.....................
});
});
// PHP
$db =&JFactory::getDBO();
$query = 'SELECT * FROM `#__virtuemart_countries` WHERE virtuemart_country_id >= "1" AND virtuemart_country_id <= "30"';
$db->setQuery($query);
$result = $db->loadObjectList();
$si = [];
$sk = [];
foreach($result as $row) {
$si[$row->virtuemart_country_id] = $row->country_3_code;
$sk[$row->virtuemart_country_id] = $row->country_3_code;
};
// JS
jQuery(function($) {
$('body').on('change', '#virtuemart_country_id', function() {
var ssnull = '',
s = {
i: <?php echo json_encode($si) ?>,
k: <?php echo json_encode($sk) ?>,
},
selector = {
i: $('#inn_field'),
k: $('#kpp_field'),
}
val = $(this).val();
if (val == 0) {
selector.i.val($ssnull);
selector.k.val($ssnull);
return;
}
$.each(s, function(key, data) {
$.each(data, function(index, value) {
if(val == index + 1) {
selector[key].val(value);
}
});
});
});
});
document.getElementById('virtuemart_country_id').onchange = function(e)
{
...
}
jQuery(function($) {
$('body').on('change', '#virtuemart_country_id', function() {
$ssnull = '';
for (var j = 1; j < 30; j++) {
window['$s'+j+'_i'] = '<?php echo $s'+j+'_i ?>'; window['$s'+j+'_k'] = '<?php echo $s'+j+'_k ?>';
}
if($(this).val() == 0) {$('#inn_field').val($ssnull); $('#kpp_field').val($ssnull);}
for (var x = 1; x < 30; x++) {
if($(this).val() == x) {$('#inn_field').val( window['$s'+x+'_i']); $('#kpp_field').val( window['$s'+j+'_k']);}
}
}
)});