Source language: Translate to:

Preserve variable values

General questions about NeoAppBuilder - our rapid application development tool for building HTML5, web and mobile apps.

Moderator: Neosoft Support

Preserve variable values

Postby David de Argentina » Mon Feb 23, 2015 11:48 pm

Hi all,

My app uses some data that i need preserve the inicial values.

I do this script (JS)

Syntax: [ Download ] [ Hide ]
Using Javascript Syntax Highlighting
var i;
var cual = "";
var P = [];
for (i = 11; i < 18; i++) {
  cual = "C" + i;
  P[i]  = document.getElementById(cual).checked;
}

alert(P[11] + " " + P[12] + " " + P[13] + " " + P[14] + " " + P[15] + " " + P[16] + " " + P[17]);
Parsed in 0.004 seconds, using GeSHi 1.0.8.10



Ok, in first time all works fine.

Now, after some changes, i need compare initial values with actual values:
if some values were changed, i need to process the changes, via php: (other values must be ignored)


Syntax: [ Download ] [ Hide ]
Using Javascript Syntax Highlighting
var i;
var cual = "";

for (i = 11; i < 18; i++) {
  cual = "C" + i;
  var estado = document.getElementById(cual).checked;
  if (estado == P[i]) {
    var donde = "http://myurl.com/update.php?user_id=" + i + "&estado=" + estado;
    $.get(donde, function(data,status){
    $('#Container2').html(data);
    },'text');
  }
}
Parsed in 0.004 seconds, using GeSHi 1.0.8.10


Problem is when i execute this last code, i receive this error message:
ReferenceError: P is not defined

But P is defined into the first code !

If i delete the codeblock

Code: Select all
if (estado == P[i]) { ...}


and execute the php directly all works fine... but execute all records of the table ( with and without changes...)

Any ideas ?

TIA

David de Argentina
User avatar
David de Argentina
 
Posts: 1553
Joined: Mon Apr 04, 2005 4:13 pm
Location: Buenos Aires, Argentina

Re: Preserve variable values

Postby Gaev » Tue Feb 24, 2015 7:46 am

David de Argentina:

Problem is when i execute this last code, i receive this error message:
ReferenceError: P is not defined


It has to do with the way Javascript assigns the scope (global vs. local) of variables.

It can be quite complex ... do a Google search on scope of Javascript variables to see how complex.

It looks like (somehow) Javascript is concluding (from your code in the section) that there is a P variable with a local scope ... with/without the one with the Global scope.

1) Perhaps the P variable that you created in the first code set was assigned a local scope ... and therefore did not have a life beyond the servicing of that routine.

In order to check this, create a Push Button .. and in its click section do ...
Code: Select all
alert(P[11] + " " + P[12] + " " + P[13] + " " + P[14] + " " + P[15] + " " + P[16] + " " + P[17]);
... if the resulting popup message contains "undefined" where there should be real values, it means that the P variable had local scope.

Make sure the P variable is first defined in the start-up section of the Application ... Project >>> App Events >>> start-up ...
Code: Select all
BeginJS
   P = [];
EndJS



2) Otherwise, is there any code (other than that you posted) in your "compare" function ? ... if so, any reference to ...

var P

... even if placed after your posted code ... can cause Javascript to ...

- create a P variable with undefined value and local scope
- use it in all commands where you reference P
User avatar
Gaev
 
Posts: 3716
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: Preserve variable values

Postby Neosoft Support » Tue Feb 24, 2015 11:10 am

As Gaev points out the variable p is local to the first function. It only exits within that function. If you remove "var" from the declaration of p then it will become a global variable.

Here is a short description of global vs local variables in JavaScript:

http://www.w3schools.com/js/js_scope.asp
NeoSoft Support
Neosoft Support
NeoSoft Team
 
Posts: 5593
Joined: Thu Mar 31, 2005 10:48 pm
Location: Oregon, USA


Return to General NeoAppBuilder Discussion

Who is online

Users browsing this forum: No registered users and 0 guests