Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

Save a value on edit form

Questions related to using nuBuilder Forte.
marcvander
Posts: 101
Joined: Mon Mar 26, 2018 5:57 pm

Save a value on edit form

Unread post 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 ?
Config:
nuBuilder v4 on a dedicated LAMP server:
-Ubuntu 14.04.5 LTS
-Apache 2.4.7
-MySQL 14.14 Distrib 5.7.22
-PHP 5.5.9-1ubuntu4.23
admin
Site Admin
Posts: 2814
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: Save a value on edit form

Unread post 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
marcvander
Posts: 101
Joined: Mon Mar 26, 2018 5:57 pm

Re: Save a value on edit form

Unread post 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)
?
Config:
nuBuilder v4 on a dedicated LAMP server:
-Ubuntu 14.04.5 LTS
-Apache 2.4.7
-MySQL 14.14 Distrib 5.7.22
-PHP 5.5.9-1ubuntu4.23
toms
Posts: 785
Joined: Sun Oct 14, 2018 11:25 am

Re: Save a value on edit form

Unread post 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
marcvander
Posts: 101
Joined: Mon Mar 26, 2018 5:57 pm

Re: Save a value on edit form

Unread post 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();
}
Config:
nuBuilder v4 on a dedicated LAMP server:
-Ubuntu 14.04.5 LTS
-Apache 2.4.7
-MySQL 14.14 Distrib 5.7.22
-PHP 5.5.9-1ubuntu4.23
admin
Site Admin
Posts: 2814
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: Save a value on edit form

Unread post 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
marcvander
Posts: 101
Joined: Mon Mar 26, 2018 5:57 pm

Re: Save a value on edit form

Unread post 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?
You do not have the required permissions to view the files attached to this post.
Config:
nuBuilder v4 on a dedicated LAMP server:
-Ubuntu 14.04.5 LTS
-Apache 2.4.7
-MySQL 14.14 Distrib 5.7.22
-PHP 5.5.9-1ubuntu4.23
toms
Posts: 785
Joined: Sun Oct 14, 2018 11:25 am

Re: Save a value on edit form

Unread post 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();
    }
}
marcvander
Posts: 101
Joined: Mon Mar 26, 2018 5:57 pm

Re: Save a value on edit form

Unread post 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
Config:
nuBuilder v4 on a dedicated LAMP server:
-Ubuntu 14.04.5 LTS
-Apache 2.4.7
-MySQL 14.14 Distrib 5.7.22
-PHP 5.5.9-1ubuntu4.23
toms
Posts: 785
Joined: Sun Oct 14, 2018 11:25 am

Re: Save a value on edit form

Unread post by toms »

You can't use nuAddJavascript() in After Save PHP. Try Before Edit PHP.
Locked