Update Select options based on date range change ?

example, Control Application

Field Date: {from}
Field Date: {to}
Field Select: {company}

Field company SQL Select statement:

SELECT id, company_name
FROM customers
WHERE id IN (SELECT company FROM orders WHERE pick_up_date >= '{from}' AND pick_up_date <= '{to}')
ORDER BY company_name

when the date changes, to update the company select options…
field Date do not have Ajax Processing…

Is there any possibility?

which AJAX event you have used? Try another event to check if AJAX works. I saw some topic in the forum referring to problems with the AJAX events

Hi,

Field Date do not have Ajax Processing, but I found another way. Here’s how, maybe somebody needs it.
These are my codes:

First Field Date: {from}
Second Field Date: {to}
Select Field: {company}

Ajax Event: from_OnChange and to_OnChange:

if ({from} && {to}){
$firm = F_MYSQL("SELECT * FROM customers WHERE id IN (SELECT company FROM orders WHERE company !='' AND pick_up_date >= '".{from}."' AND pick_up_date <= '".{to}."')");

//This returns is one array, then we convert it to JSON with json_encode and we send it to javascript method getCompany  to fill select field company

sc_ajax_javascript( 'getCompany ', array(json_encode($firm),'0'));

    }else{
sc_ajax_javascript( 'getCompany ', array(null,'1'));

}

JavaScript Methods: getCompany with parameters js and cl

if (cl == '1'){
let dropdown = $('#id_sc_field_company');
dropdown.empty();
dropdown.append('<option selected="true">Firma ausw?hlen...</option>');
}else{

let dropdown = $('#id_sc_field_company');

dropdown.empty();

dropdown.append('<option selected="true">Firma ausw?hlen...</option>');
dropdown.prop('selectedIndex', 0);
console.log(js);
// Populate dropdown with list of provinces
  $.each(JSON.parse(js), function (key, entry) {
    dropdown.append($('<option></option>').attr('value', entry.id).text(entry.company_name));
  });
}

PHP Method: F_MYSQL

function F_MYSQL($query){
$mysqli = mysqli_connect([sc_glo_server], [sc_glo_user], sc_decode([sc_glo_pass]), [sc_glo_db]) or die('Unable to Connect');
if ($mysqli->connect_error) {
    die('Error : (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}
       $mysqli->query("SET NAMES 'utf8'");

    $fetch1 = $mysqli->query($query) or trigger_error($mysqli->error);
    $rows1 = array();
    while ($r = mysqli_fetch_assoc($fetch1)) {
    $rows1[] = $r;
    }
    return $rows1;
}

I hope I helped someone :slight_smile:

1 Like

very useful!!