Page 1 of 2

Save a value on edit form

Posted: Tue Jun 26, 2018 6:36 pm
by marcvander
Hey,

I have this working:
-when browsing all the companies on the Company form, after clicking on one and accessing Edit form, I can create an employee which will automatically be assigned to this company with this code:

On Company form:

Code: Select all

function nuSelectBrowse(e) {
    var r   = $('#' + e.target.id).attr('data-nu-primary-key');
    window.ENTREPRISE_ID = r;
    nuForm(nuGetProperty('form_id'), r);   
}
On employee form:

Code: Select all

if (nuFormType() == 'edit') {
    $("#contact_entreprise_id").val(window.ENTREPRISE_ID).change();
    nuHasNotBeenEdited();
}
The issue is if I create a new company (I don't select one in the browse form), and then create an employee from here, no company is assigned to the new contact since the value ENTREPRISE_ID didn't get saved with the function nuSelectBrowse(). Is there any way to add a function so that after creating a new company, the value ENTREPRISE_ID is also saved, so that it is automatically assigned to a new contact created right after ?

Re: Save a value on edit form

Posted: Wed Jun 27, 2018 3:12 am
by admin
marcvander,

You can update a record after it is saved in After Save using Hash Cookies.

https://wiki.nubuilder.cloud/ ... s#PHP_Code

With something like this...

https://wiki.nubuilder.cloud/index.php/PHP


Steven

Re: Save a value on edit form

Posted: Wed Jun 27, 2018 11:59 am
by marcvander
Thanks Steven for the answer.

Since on my employee form, the JS code is

Code: Select all

if (nuFormType() == 'edit') {
    $("#contact_entreprise_id").val(window.ENTREPRISE_ID).change();
    nuHasNotBeenEdited();
}
I shall assign the value of company name to window.ENTREPRISE_ID right ?
So I have to do this in the PHP After Save code ? With a function like nuHash() ?

Like

Code: Select all

window.ENTREPRISE_ID = nuHash(entreprise_nom)
?

Re: Save a value on edit form

Posted: Thu Jun 28, 2018 5:13 pm
by toms
marcvander wrote: So I have to do this in the PHP After Save code ? With a function like nuHash() ?
Yes, this is where you can update a record using an update query:

e.g.

Code: Select all

$sql = "UPDATE table_name SET your_field = '123' WHERE record_id_field = '#record_id#' ";

nuRunQuery($sql);
You can use nuHash(), inside nuDebug() to find what Hash Cookies are available.

https://wiki.nubuilder.cloud/ ... PHP#nuHash

https://wiki.nubuilder.cloud/ ... HP#nuDebug

Re: Save a value on edit form

Posted: Fri Jun 29, 2018 7:34 pm
by marcvander
Since my JS code is:

Code: Select all

if (nuFormType() == 'edit') {
    $("#contact_entreprise_id").val(window.ENTREPRISE_ID).change();
    nuHasNotBeenEdited();
}
The value of the newly created company should be stored in the variable window.ENTREPRISE_ID.

So my PHP to run After Save would be:

Code: Select all

$s = "SELECT * FROM entreprise WHERE entreprise_id=#RECORD_ID# ";
$t = nuRunQuery($s);
$window.ENTREPRISE_ID = db_fetch_object($t)->entreprise_id;


Or more something like this ? :

Code: Select all

$s = "SELECT * FROM entreprise WHERE entreprise_id=#RECORD_ID# ";
$t = nuRunQuery($s);
$e = db_fetch_object($t)->entreprise_id;

$j = "

function fill(){
   return '$e';    
}
";
And then the JS like this ? :

Code: Select all

if (nuFormType() == 'edit') {
    $("#contact_entreprise_id").val(fill()).change();
    nuHasNotBeenEdited();
}

Re: Save a value on edit form

Posted: Sat Jun 30, 2018 12:03 am
by admin
marcvander,

I think toms answer is what you are looking for except that #record_id# might work better as #RECORD_ID#
$sql = "UPDATE table_name SET your_field = '123' WHERE record_id_field = '#record_id#' ";

nuRunQuery($sql);
Steven

Re: Save a value on edit form

Posted: Sun Jul 01, 2018 12:36 pm
by marcvander
Steven and toms, I think my question was not clear enough. Let me try to reformulate. I am not trying to update a field during After Save. I am trying to populate a field with a value in the front end only (so Javascript).

Goal:
I'm trying to populate a field on my Contact form.

Procedure:
Users browse Companies. After clicking on a company, along with all the info of the company they have a button to "Create a new contact":
Capture d’écran 2018-07-01 à 12.21.45.png
This button is set up like this (so that it arrives right away on Add a new entry):
Capture d’écran 2018-07-01 à 12.20.47.png
Once they clicked on "Create a new contact" and arrive on the Contact form to add a new contact, I would like to populate the field entreprise_id with the value of the company they were looking at before clicking on "Create a new contact". I'm doing it so far with this JS on the Company form:

Code: Select all

function nuSelectBrowse(e) {
    var r   = $('#' + e.target.id).attr('data-nu-primary-key');
    window.ENTREPRISE_ID = r;
    nuForm(nuGetProperty('form_id'), r);   
}
And this JS on the Contact form:

Code: Select all

if (nuFormType() == 'edit') {
    $("#contact_entreprise_id").val(window.ENTREPRISE_ID).change();
    nuHasNotBeenEdited();
}
(This code was suggested by toms on works well).

The problem is that it only works when the user is browsing already existing companies (because of the function nuSelectBrowse()). So when the user creates a new company and saves it, and clicks on "Create a new contact", the function nuSelectBrowse() is not executed, so there is no entreprise_id populated in the Contact form.

So my question was: how can I populate this entreprise_id field on the Contact form after a new company is created and the button "Create a new contact" is clicked?

Re: Save a value on edit form

Posted: Mon Jul 02, 2018 10:34 am
by toms
I'm not sure if I understand 100% what you're trying to do..

In the contact form, you could get the entreprise_id of the parent window and then assign it to contact_entreprise_id if it's empty.

Code: Select all

if (nuFormType() == 'edit') {
    var id = $("#entreprise_id", window.parent.document);
    if (id.length == 1 && $("#contact_entreprise_id").val().length == 0) {
        $("#contact_entreprise_id").val(id.val()).change();
        nuHasNotBeenEdited();
    }
}

Re: Save a value on edit form

Posted: Mon Jul 02, 2018 1:36 pm
by marcvander
Toms, here is what I was trying to do:

Add on the Enterprise form After Save PHP:

Code: Select all

$select2 = "SELECT * FROM entreprise WHERE entreprise_id='#RECORD_ID#'";
$run2 = nuRunQuery($select2);
$o2 = db_fetch_object($run2);
$entreprise = $o2->entreprise_id;

$j2 = "

function entrepriseId(){
   return '$entreprise';    
}

";

nuAddJavascript($j2);
And on the JS on the Contact form:

Code: Select all

if (nuFormType() == 'edit') {
    $("#contact_entreprise_id").val(entrepriseId()).change();
    nuHasNotBeenEdited();
};
But I have this error in console:

entrepriseId() is not defined

Re: Save a value on edit form

Posted: Mon Jul 02, 2018 1:56 pm
by toms
You can't use nuAddJavascript() in After Save PHP. Try Before Edit PHP.