Page 1 of 2
Default values
Posted: Thu Jun 28, 2018 3:13 pm
by marcvander
Hey,
I'm trying to set a default value for an object on an edit form. I found this topic:
https://forums.nubuilder.cloud/viewtopic. ... lue#p17268
The code suggested is this one:
Code: Select all
if (nuFormType() == 'edit') {
$('#your_field').val("hello").change();
nuHasNotBeenEdited();
});
But I would like to set a default value based on who is the current logged in user adding a new entry. In my database, I have a table called employee. In this table, there is a foreign key employee_user pointing to zzzzsys_user_id of table zzzzsys_user. So I can assign an employee to a user. In my employee table I have two fields employee_fr (tinyint) and employee_ch (tinyint). Both can either be 0 or 1. Based on if the value is 0 or 1 for the current logged in user, I would like to enter a default value on an edit form. How can I do that ? (in nuBuilder v3, I could do it easily because there was an SQL entry for default values on any object, so I could query the right info. I guess for v4, I have to have a php procedure running in the back ?)
Re: Default values
Posted: Thu Jun 28, 2018 4:22 pm
by toms
Hi,
Something like this? (untested)
In the Before Edit (PHP) Event:
Code: Select all
$s = "SELECT * FROM .... ";
$t = nuRunQuery($s);
$ch = db_fetch_object($t)->employee_ch;
$fr = db_fetch_object($t)->employee_fr;
$j = "
function employee_fr(){
return '$fr';
}
function employee_ch(){
return '$ch';
}
";
And then the JS in your form:
Code: Select all
if (nuFormType() == 'edit') {
if (employee_ch() == "1") {
$('#your_field').val("hello").change();
}
if (employee_fr) == "1") {
$('#your_field').val("hello").change();
}
nuHasNotBeenEdited();
});
Re: Default values
Posted: Fri Jun 29, 2018 7:13 pm
by marcvander
Toms, thanks for the reply. There was a typo here:
I changed it to
Here is the error I get in console when I go on the form:
Capture d’écran 2018-06-29 à 19.06.36.png
Maybe the problem comes from the fact that you declare the variable $j in the PHP in which you have the two JS functions, but you don't use this variable anywhere else. So the functions are unknown (it's just a guess, i'm not a pro in neither PHP nor JS).
Re: Default values
Posted: Fri Jun 29, 2018 7:51 pm
by toms
There's a bracket too much:
Code: Select all
if (nuFormType() == 'edit') {
if (employee_ch() == "1") {
$('#your_field').val("hello").change();
}
if (employee_fr) == "1") {
$('#your_field').val("hello").change();
}
nuHasNotBeenEdited();
};
Re: Default values
Posted: Fri Jun 29, 2018 11:59 pm
by admin
.
Re: Default values
Posted: Sun Jul 01, 2018 1:00 pm
by marcvander
Toms, thanks. Indeed there was a bracket too much. Though now i have this error:
Capture d’écran 2018-07-01 à 12.47.17.png
To solve it I tried to add a
at the end, but I still have the same error. My code now looks like this:
PHP Before Edit:
Code: Select all
$select = "SELECT * FROM employe WHERE employe_user=#USER_ID#";
$run = nuRunQuery($select);
$ch = db_fetch_object($run)->employe_ch;
$fr = db_fetch_object($run)->employe_fr;
$j = "
function employe_fr(){
return '$fr';
}
function employe_ch(){
return '$ch';
}
";
nuJavascriptCallback($j);
The JS on the form to put default values:
Code: Select all
if (nuIsNewRecord()) {
if (employe_ch() == "1") {
$('#contact_ch').val("1").change();
}
if (employe_fr() == "1") {
$('#contact_fr').val("1").change();
}
nuHasNotBeenEdited();
};
Re: Default values
Posted: Sun Jul 01, 2018 1:30 pm
by toms
#USER_ID# must be enclosed in single quotation marks
Code: Select all
$select = "SELECT * FROM employe WHERE employe_user= '#USER_ID#' ";
Re: Default values
Posted: Sun Jul 01, 2018 2:28 pm
by marcvander
Toms, indeed, thanks. But I still have the same error: employe_ch is not defined. The system somehow cannot find the functions employe_ch and employe_fr that we created in the variable $j. Was it right to add the nuJavascriptCallback function at the end of the PHP ? Is there anything else that we have to add ?
Re: Default values
Posted: Sun Jul 01, 2018 8:21 pm
by toms
marcvander wrote:Was it right to add the nuJavascriptCallback function at the end of the PHP ? Is there anything else that we have to add ?
nuJavascriptCallback() is used together with nuRunPHPHidden(). You need to use nuAddJavascript($j); And call db_fetch_object() just once.
Code: Select all
$run = nuRunQuery($select);
$select = "SELECT * FROM employe WHERE employe_user= '#USER_ID#' ";
$run = nuRunQuery($select);
$o = db_fetch_object($run);
$ch = $o->employe_ch;
$ch = $o->employe_fr;
$j = "
function employe_fr(){
return '$fr';
}
function employe_ch(){
return '$ch';
}
";
nuAddJavascript($j);
Re: Default values
Posted: Sun Jul 01, 2018 8:47 pm
by marcvander
Toms, thanks, nuAddJavascript($j) made it work
Now I have an issue to populate my form. I'm trying to populate two checkboxes:
Capture d’écran 2018-07-01 à 20.42.25.png
The JS code is:
Code: Select all
if (nuIsNewRecord()) {
if (employe_ch() == 1) {
$('#contact_ch').val(1).change();
}
if (employe_fr() == 1) {
$('#contact_fr').val(1).change();
}
nuHasNotBeenEdited();
};
But the checkboxes do not populate. I used the console to test my JS code, here is the result:
Capture d’écran 2018-07-01 à 20.41.36.png
So it shows that the conditions work and should populate. So the issue is on this line I guess:
I also tried:
Code: Select all
$('#contact_ch').val("1").change();
And:
Code: Select all
$('#contact_ch').val('1').change();
Maybe to populate a checkbox, .val().change() doesn't work?