change value in a filed when select another field

If you're a new user of AppGini, feel free to ask general usage questions, or look for answers here.
Post Reply
landrea
Posts: 26
Joined: 2018-08-04 13:09

change value in a filed when select another field

Post by landrea » 2019-03-14 18:21

I made the hook course only I forget lot of things because I don't use the software often
I need a little help to remember best techinique to do it

I have a table with a lookup filed point into another table,
this filed is a select
now in the next field it's readonly field i need to store something is in the foreign table,
so when the user select, I look the fk stored in the select field, I get the value of the other field in foreign table and I write it in the readonly field in the main table

now, I'm preety sure in the course we had something like this,
I totally forget, I foget If I need the table after insert hook or i need to use something like the ajax call (the calculation example)

pböttcher
AppGini Super Hero
AppGini Super Hero
Posts: 410
Joined: 2018-04-01 10:12

Re: change value in a filed when select another field

Post by pböttcher » 2019-03-14 18:35

Hi,

you need to use the ajax call. You can use the hook file, or create the <tablename>-dv.js file (in the hooks folder).

BTW. If you retrieve the value and put it in a read only field, be aware that this value will NOT be stored into the field in the database, so next time you view the record, you need to retrieve the value against the fk stored in the dropdown field.

landrea
Posts: 26
Joined: 2018-08-04 13:09

Re: change value in a filed when select another field

Post by landrea » 2019-03-14 18:45

thanks very much, I think is in section 10 of the course
I need to adapt a little the example probably because had to do with calculation,
more or less the select should fire a ajax cal and then I write the value in the field with jquery,
I need a php file inside hook that create the sql

so, it's question to study a little the example,
but that's not have to do with the table hook before insert, or after insert,
I was a little confused, now I remember better what I have to do

about your suggestion, I didn't know this, I see there is a readonly field inthe options in appgini,
I change in that case and I simply use js to create the read only filed, but I store data as normal string

I need it for the search, that's why I need to store more data from the foreign key, that's the plan at least

pböttcher
AppGini Super Hero
AppGini Super Hero
Posts: 410
Joined: 2018-04-01 10:12

Re: change value in a filed when select another field

Post by pböttcher » 2019-03-14 20:29

ok, have fun. If you need further help, post it. Best is with sample and/or screenshots if possible.

landrea
Posts: 26
Joined: 2018-08-04 13:09

Re: change value in a filed when select another field

Post by landrea » 2019-03-15 15:00

ok, I made it, I have a problem because that's not a real select
ajax call is ok so I don't post it

If I look the html code in console the value of the field connected the select is ok, it change correctly,
but when I console.log (in my script onchange jquery) I'm getting alwasy the precedent selection (the fk id i send in the ajax) , so it works, because I'm writing in the form, in the onready input, but I return in the ajax call the value of the precedent selection, always the precedent, in some way is too fast to look the value in the onchange event after it really change

Now, I tryed to fire the event on the input field instead on the select (that's not a real html select, but ok, I dont' know how to call it) and happen nothing, because I think you need to focus there, so now I'm going to move the onchange on this field and try to focus it from the select and fire another onchange function

I was looking an event that simply look if a input value change by code, but I'm not sure exist,
that's a little strange, I know better vanilla js and you dont' have problem of asynchronism usually

I think this select work a little in its own way, so I need to find a workaround, I try to focus this field with jquery and see what happen


dt_aule-dv.js
// ==================================================
// MESSAGGIO DI ERRORE
function show_error(field, msg){
modal_window({
message: '<div class="alert alert-danger">' + msg + '</div>',
title: 'Errore in ' + field,
close: function(){
$j('#' + field).focus();
$j('#' + field).parents('.form-group').addClass('has-error');
}
});

return false;
} // FINE SHOW ERROR

// ==================================================
// INIZIALIZZA ONCHANGE - SETUP ONREAD ONLY - JQUERY
$j(function(){

// READ ONLY
$j('#ordine_plesso_fk').attr('readonly','readonly');

// FUNZIONE ON CHANGE SELECT
$j('#plesso_nome_fk-container').on('change', function(){

// ripulisco
$j('#ordine_plesso_fk').val('');

var aula_id_to_send = $j('#plesso_nome_fk').val();

console.log(aula_id_to_send);

// PREDISPONE CHIAMATA AJAX - FILE IN HOOKS - AJAX_AULE_SELECT.PHP
$j.ajax({
url: 'hooks/ajax_aule_select.php',
data: { aula_id: aula_id_to_send },
success: function(data){
$j('#ordine_plesso_fk').val(data);
}
});

}) // FINE FUNZIONE ONCHANGE

}) // FINE FUNZIONE INIZIALIZZA
// ====================================================

landrea
Posts: 26
Joined: 2018-08-04 13:09

Re: change value in a filed when select another field

Post by landrea » 2019-03-15 15:22

at the moment I changed in this way, adding a deley of 2 seconds, then I'm going to add a required field there with jquery or directly in the software menu of appgini, so you can't submit if is still empty, I woudl prefer another soluction, even though in my test 2 seconds are probably sufficient to get the right value, here the problem is the asynchrosnm of the select, I don't know if I can solve in other way, if workaround is ok and there aren't better soluction I'm going to setup this way for doing the job

// ==================================================
// MESSAGGIO DI ERRORE
function show_error(field, msg){
modal_window({
message: '<div class="alert alert-danger">' + msg + '</div>',
title: 'Errore in ' + field,
close: function(){
$j('#' + field).focus();
$j('#' + field).parents('.form-group').addClass('has-error');
}
});

return false;
} // FINE SHOW ERROR

// ==================================================
// INIZIALIZZA ONCHANGE - SETUP ONREAD ONLY - JQUERY
$j(function(){

// READ ONLY
$j('#ordine_plesso_fk').attr('readonly','readonly');


// FUNZIONE ON CHANGE SELECT
$j('#plesso_nome_fk-container').on('change', function(){

// ripulisco
$j('#ordine_plesso_fk').val('');

function preleva_valore () {

var aula_id_to_send = $j('#plesso_nome_fk').val();

console.log(aula_id_to_send);

// PREDISPONE CHIAMATA AJAX - FILE IN HOOKS - AJAX_AULE_SELECT.PHP
$j.ajax({
url: 'hooks/ajax_aule_select.php',
data: { aula_id: aula_id_to_send },
success: function(data){
$j('#ordine_plesso_fk').val(data);
}
});

} // fine funzione preleva valore delay 2s

setInterval(preleva_valore,2000);

}) // FINE FUNZIONE ONCHANGE

}) // FINE FUNZIONE INIZIALIZZA
// ====================================================

landrea
Posts: 26
Joined: 2018-08-04 13:09

Re: change value in a filed when select another field

Post by landrea » 2019-03-15 15:39

I share the last version, in case people have same need or people want suggest something to do it better,
I added an onlick function that show the modal if the field is empty, so dont' store data if the delay still is working and user fast push save button


dt_aule-dv.js
// ==================================================
// MESSAGGIO DI ERRORE
function show_error(field, msg){
modal_window({
message: '<div class="alert alert-danger">' + msg + '</div>',
title: 'Errore in ' + field,
close: function(){
$j('#' + field).focus();
$j('#' + field).parents('.form-group').addClass('has-error');
}
});

return false;
} // FINE SHOW ERROR


// ==================================================
// INIZIALIZZA ONCHANGE - SETUP ONREAD ONLY - JQUERY
$j(function(){


// READ ONLY
$j('#ordine_plesso_fk').attr('readonly','readonly');



// PREVIENE SALVATAGGIO SE CAMPO E' VUOTO
$j('#update').click(function(){

var verifica_input_ordine = $j('#ordine_plesso_fk').val();

if(verifica_input_ordine == ''){
return show_error('Ordine del plesso', 'Il campo ordine non può essere vuoto');
}

}) // fine funzione update



// FUNZIONE ON CHANGE SELECT
$j('#plesso_nome_fk-container').on('change', function(){

// ripulisco
$j('#ordine_plesso_fk').val('');

function preleva_valore () {

var aula_id_to_send = $j('#plesso_nome_fk').val();

console.log(aula_id_to_send);

// PREDISPONE CHIAMATA AJAX - FILE IN HOOKS - AJAX_AULE_SELECT.PHP
$j.ajax({
url: 'hooks/ajax_aule_select.php',
data: { aula_id: aula_id_to_send },
success: function(data){
$j('#ordine_plesso_fk').val(data);
}
});

} // fine funzione preleva valore delay 2s

setInterval(preleva_valore,2000);

}) // FINE FUNZIONE ONCHANGE

}) // FINE FUNZIONE INIZIALIZZA
// ====================================================



ajax_aule_select.php
<?php
$currDir = dirname(__FILE__) . '/..';
include("$currDir/defaultLang.php");
include("$currDir/language.php");
include("$currDir/lib.php");

/* PERMESSO DI VISUALIZZAZIONE - TABELLA */
$od_from = get_sql_from('dt_aule');
if(!$od_from){
header('HTTP/1.0 401 Unauthorized');
exit;
}

// INTERCETTA FK AULA
$aula_id = intval($_REQUEST['aula_id']);

$ordine = sqlValue("select ordine_plesso from dt_plessi where id_plesso='{$aula_id}'");

echo $ordine;
?>

Post Reply