Custom Code inconsistent results

Questions related to customising nuBuilder Forte with Javascript or PHP.

Custom Code inconsistent results

Unread postby icoso » Wed Jun 02, 2021 7:28 am

I have a launch form that has several fields on it that I used for searching my data table. The form has a button on it and the code in the button is as follows:

Code: Select all
var newssn = $('#FRM_ssn').val();
var lastname = $('#FRM_lastname').val();
var firstname = $('#FRM_firstname').val();
var sql = '';

if (newssn !== "") {
    nuSetProperty('nuSSNField', newssn, true);
    nuRunPHPHidden("encryptSSN",0);
    //alert("Enc SSN="+window.newencssn);
    sql = sql + " AND TaxCustomers.cust_prissn = '" + newencssn +"'";
} else {

if (lastname !== "") {
    sql = sql + " AND TaxCustomers.cust_lastname LIKE '" + lastname +"%'";
}   
if (firstname !== "") {
    sql= sql + " AND TaxCustomers.cust_firstname LIKE '" + firstname +"%'";
}   
}
   sql = sql.substr(5);
   nuSetProperty('FRM_search', sql, true);
   nuSetProperty('FRM_srchsqlform', 'Y', true);
   nuSetProperty('SEARCH_FIELD', '');

alert('Form SQL='+sql);

if (sql !== "" ) {
nuForm('60887a31411ae22', '', '', '', '0');
}


The PHP Code that gets called by the nuRunPHPHidden("encryptSSN",0); is as follows:

Code: Select all
$f = __DIR__ . "/proc_mxssn.php";
// nuDebug("Directory= ".$f);
include_once($f);
$e = mixSSN("#nuSSNField#");
$js =
"
window.newencssn = '$e';
";
nuDebug("In EncryptSSN =".$js);
nuJavascriptCallback($js);


This php code WORKS It encrypts my nuSSNField, My NuDebug Results shows the correct value.

Here's the problem: In the Javascript above that is on my button on my form, if I unremark the //alert("Enc SSN="+window.newencssn); I do not get anything for the variable. HOWEVER, the next line that sets the SQL variable works based on the alert: alert('Form SQL='+sql); that is executed right before the search form is called. If I remark out the //alert("Enc SSN="+window.newencssn); The SQL variable DOES not get set properly. The newencssn variable has nothing in it. WHY?

Am I not transferring the results properly from the call to: nuRunPHPHidden("encryptSSN",0); ?
Why is the newencssn variable accessible if I dont have the alert remarked, btu it is not or its empty if I have the alert remarked out?
icoso
 
Posts: 173
Joined: Mon Feb 08, 2021 7:39 am
nuBuilder Version: 4.5

Re: Custom Code inconsistent results

Unread postby kev1n » Wed Jun 02, 2021 7:52 am

Asynchronous calls do not block (or wait) for the call to return from the server, execution continues.

The code that has to be executed after running nuRunPHPHidden() can be moved to a function.

JS:

Code: Select all
var newssn = $('#FRM_ssn').val();
var lastname = $('#FRM_lastname').val();
var firstname = $('#FRM_firstname').val();
var sql = '';

if (newssn !== "") {
    nuSetProperty('nuSSNField', newssn, true);
    nuRunPHPHidden("encryptSSN", 0);   
} else {

    if (lastname !== "") {
        sql = sql + " AND TaxCustomers.cust_lastname LIKE '" + lastname + "%'";
    }
    if (firstname !== "") {
        sql = sql + " AND TaxCustomers.cust_firstname LIKE '" + firstname + "%'";
    }
   
   somemeaningfulname();
}



function somemeaningfulname(newencssn = '') {

   if (newencssn !== '') {
      sql = sql + " AND TaxCustomers.cust_prissn = '" + newencssn + "'";
   }
   
   sql = sql.substr(5);
   nuSetProperty('FRM_search', sql, true);
   nuSetProperty('FRM_srchsqlform', 'Y', true);
   nuSetProperty('SEARCH_FIELD', '');

   alert('Form SQL=' + sql);

   if (sql !== "") {
      nuForm('60887a31411ae22', '', '', '', '0');
   }

}


PHP:

Code: Select all
$f = __DIR__ . "/proc_mxssn.php";
// nuDebug("Directory= ".$f);
include_once($f);
$e = mixSSN("#nuSSNField#");

$js = "somemeaningfulname('$e'); ";
nuJavascriptCallback($js);


(code untested, just typed here)
kev1n
nuBuilder Team
 
Posts: 1763
Joined: Mon Oct 15, 2018 2:13 am
nuBuilder Version: 4.5

Re: Custom Code inconsistent results

Unread postby icoso » Wed Jun 02, 2021 9:17 am

This doesn't seem to work. It appears that the function $js = "somemeaningfulname('$e'); "; doesn't get executed from that PHP code. I put an alert as the first line in that function and if I put an SSN in my form and it does go and run the encryption PHP code, the debug shows the correct data:" In EncryptSSN MixedSSN=finishSQL('39999311333499999331339998'); " so I know the PHP is running, but apparently the function doesn't get called. Could it be that the nuJavascriptCallback($js); simply doesn't see the function I have defined in that button's Custom Code? I even tried moving the function to the beginning of the javascript code so that it would be defined before the call to nuRunPHPHidden("encryptSSN", 0); occurs.

I search with just a last name or first name, it works fine. But doesn't work if I put in an SSN.

Any ideas?
icoso
 
Posts: 173
Joined: Mon Feb 08, 2021 7:39 am
nuBuilder Version: 4.5

Re: Custom Code inconsistent results

Unread postby kev1n » Wed Jun 02, 2021 9:28 am

Try moving the button's Custom Code (CC) to the form's CC.
In the Button's CC you would simply call a function that is declared in the form's CC.
kev1n
nuBuilder Team
 
Posts: 1763
Joined: Mon Oct 15, 2018 2:13 am
nuBuilder Version: 4.5

Re: Custom Code inconsistent results

Unread postby icoso » Wed Jun 02, 2021 10:40 am

That worked, but I had to create a 2nd function from the first one in the forms CC that I could send the sql variable to from the button's CC that generated the sql variable. Here's the function: This is call if anything else bt the SSN is completed on the search from. Thanks for the help.

Code: Select all
function callFormSQL (sql='') {

   sql = sql.substr(5);
   
   // nuSetProperty('FRM_lname', lastname, true);
   // nuSetProperty('FRM_fname', firstname, true);
   nuSetProperty('FRM_search', sql, true);
   nuSetProperty('FRM_srchsqlform', 'Y', true);
   nuSetProperty('SEARCH_FIELD', '');

if (sql !== "" ) {
nuForm('60887a31411ae22', '', '', '', '0');
} else {
   alert("You must select something to do a search...");
}
} // End callFormSQL
icoso
 
Posts: 173
Joined: Mon Feb 08, 2021 7:39 am
nuBuilder Version: 4.5


Return to Custom Code

Who is online

Users browsing this forum: No registered users and 11 guests