Jump to content
crazyswede

Modifying the value of a global variable

Recommended Posts

classic ASP - javascript

-------------------------------

var datamessage = "global"; [<--- I ESTABLISH THE GLOBAL VARIABLE]

aaa=10;

recnum=1

 

if( to_do == "EditEntry" )

{ //33

Response.Write( “before”+ recnum +") datamessage = ["+ datamessage +"] <br>" ); [<-- “global”]

datamessage = "pppppppppppppppp";

Recnum = 37;

[ It apparently doesn't know there is a global variable by the same name, so it creates a new local variable, while the global one remains unchanged. String or integer, it doesn't matter. ]

Response.Write( “after”+ recnum +") datamessage = ["+ datamessage +"] <br>" );

 <input type=hidden name="to_do"   value= "WriteEntry">

<input type=submit  value="Save">

} //33

 

if( to_do == "WriteEntry" )

{ //24B

Response.Write( “WriteEntry ”+ recnum +") datamessage = ["+ datamessage +"] <br>" );

[....global variables remain unchanged. Please, how can I fix this? .... ]

} //24B

 

Share this post


Link to post
Share on other sites

The following line of code does not make any sense:

Response.Write( “before”+ recnum +") datamessage = ["+ datamessage +"] <br>" ); 

Write the desired outcome please, and then we can figure out how to obtain it.

Roddy

 

 

Share this post


Link to post
Share on other sites

That's ASP, it makes sense in the context of ASP. It is server-side Javascript.

I have not worked much with ASP, so I don't know exactly the problem, but it is likely because you are mixing up server-side Javascript with client-side Javascript. The client does not have direct access to variables that have been declared on the server.

Share this post


Link to post
Share on other sites

Could you edit into the Original Post, what the output of this section of code is? I'm curious as to what's happening here.

Share this post


Link to post
Share on other sites

The below is the full, working code. You can copy and paste and run it, or you can run it on my website -

http://ccs2468.com/msc/genesis.test.asp?to_do=EditEntry

--------------------------------------------------------

<%@ Language=JavaScript %>

<HTML> <HEAD>

 

<TITLE>Genesis.test.asp?to_do=EditEntry</TITLE>

 

</HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">

 

<form action= "<% =  this_script_url %>" >

<% 

var this_script_url = String( Request.ServerVariables("SCRIPT_NAME"));

 

var to_do         = String( Request.QueryString( "to_do" ) );

 var datamessage = "global";;//<--- I ESTABLISH THE GLOBAL VARIABLES

Recnum=1

 

if( to_do == "EditEntry" )

{ //33

Response.Write( “before modify ”+ Recnum +") datamessage "+ datamessage +" <br>" ); //<--datamessage=  “global”, Recnum= 1,  like it should be

 

datamessage = "pppppppppppppppp"; //←- It apparently doesn't know there is a global variable by the same name, so it creates a new local variable, while the global one remains unchanged.

 

Recnum = 37; //<-- I change the value of a global integer as well, although it doesn't matter, it's the same. String or integer, it doesn't matter.

 

Response.Write( “after modify ”+ Recnum +") datamessage = "+ datamessage +" <br>" ); //← these are the values of the local variables, while the global ones remain unchanged.

 %>

<FONT size =6 FACE="Copperplate Gothic Bold"

<P ALIGN="center" ><B>Edit List</B><br>

<FONT size=5 FACE="Times New Roman" >

<p>

 <textarea cols=100 rows=20 >

<%Response.Write( datamessage ) %>

</textarea ><br>

 

<input type=hidden name="to_do"   value= "WriteEntry">

<input type=submit  value="Save">

<%

} //33

 

if( to_do == "WriteEntry" )

{ //24B

Response.Write( “in WriteEntry datamessage = "+ datamessage +" <br>" );

Response.Write( “in WriteEntry Recnum = "+ Recnum +" <br>" ); //← ....global variables remain unchanged. 

 } //24B

 

%>

 

</BODY></HTML>

 

Share this post


Link to post
Share on other sites

Those values are all set every time the page loads.  If you want values to stay across page loads, you need to use the session.  Just because you set a certain variable inside an if block on one page, when you click submit and the page submits the form and refreshes, it doesn't save anything from the previous page other than what the form submitted and whatever you saved in the session.  All variables, local and global, get destroyed when the response ends, and everything starts new then the next request is sent.

So, you can either pass values along with the rest of the form data, or you can use the session to maintain values across pages.

Share this post


Link to post
Share on other sites

It sure is a good thing you're there to help me with these things! I would never have known about sessions. So Below is what I found on the web - I get nothing but a blank screen, no errors, just a blank screen. It should print the value of sessions, shouldn’t it -  “ID” and “code?” Can you tell me what I'm doing wrong?

----------------- testSession.asp ----------

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>

<%

Session("id")=1234

Session("code")="ZZ"

%>

----------------- testSession.htm (this is what I run) ----------

<html> <head></head> <body> <script type="text/javascript" src="asp/testSession.asp">

alert("Session ID " + Session("id"));

</script> </body>

Share this post


Link to post
Share on other sites

There are several things wrong here.  One, you have a script tag pointing to an ASP page, and you're telling it that the script type is Javascript, but that ASP page does not have any Javascript output.  You also have an alert in the body of that script tag.  If you looked at your browser's developer console, you would see an error message about Session not being defined.  It is defined in ASP, not Javascript.  ASP running on the server and Javascript running in the browser are completely separate.  You need to have ASP print the value of that session variable onto the page, you cannot access it directly via Javascript.  You can not access any data structures, functions, variables, etc that are defined in ASP or any other server-side language directly from a client-side language like Javascript.  One of them runs on the server and one of them runs in the browser, they are not running on the same computer.  They don't share any memory or anything like that.

Share this post


Link to post
Share on other sites

I have a program right now that reads a file off my computer into <textarea>, I modify it, and then pass it all to another if-block, that then writes it all back to the file. And  that works. The problem arises when I try to pass too much data. I calculate that I can pass 250 characters - any more and the program will crash. What if I have a file with several thousand characters?  I thought maybe if I make my variable global, I could avoid the matter of passing anything. Global variables though are very frustrating, no one will help me, and I haven't got the time to screw around with this thing. So I was looking into “textarea,” Apparently, after I click submit, it seems that I can tell The computer to write everything to the file. Is that correct? In another program I can then open that file, get the data out, and write it all to the file.

Below are the two programs. The second program, I just print the contents of the file - but I get FILE NOT FOUND. please what am I doing wrong?

 

------------ testpass.htm --------------

<!DOCTYPE html>

<html>

<body>

 

<form action="/action_page.php">

 Text:<br>

 <textarea name="explanation"dirname="explanation.dir"></textarea>

 <input type="submit" value="Submit">

</form>

 

<p>When the form is being submitted, the text direction of the textarea will also be submitted.</p>

 

</body>

</html>

------------ testpass.asp --------------

<%@ Language=JavaScript %>

<%

var URLname  = "E:\\web\\ccs2468com0\\htdocs\\action_page.php";

 

var ForReading = 1, ForWriting = 2, ForAppending = 8;

var abs_path = String(Request.ServerVariables("PATH_TRANSLATED"));

var file_to_open =  URLname ;

fso = new ActiveXObject("Scripting.FileSystemObject"); ; //<--FILE NOT FOUND

fs_stream = fso.OpenTextFile (file_to_open, ForReading )

ExistContents = fs_stream.ReadAll(); //fs_stream.Write( MemoContents +" \r\n" );

fs_stream.close();

 Response.Write( "ExistContents= "+ ExistContents +"<br>" ); ;

 %>

Share this post


Link to post
Share on other sites

If it says the file isn't found, then the file isn't found.  You're telling it to open a file that doesn't exist.

You didn't show the PHP code for action_page.php, but all that ASP code is going to do is print out the PHP code.

If you want to submit long data, use post instead of get.  The browser and server will both impose limits on the length of the URL, and using get causes the data to be submitted in the URL.

Share this post


Link to post
Share on other sites

Got it.! I incorporated the <session> into an asp program, two if-blocks (I call those “instances”). In the first block I assign a value to my variable - (ExistingContents = “existing”). Using session object I then file that variable away in the computer. After clicking submit, the second block extracts the value and prints it - A simple little program that doesn't really do anything. At least I  now know it can be done.

 

What I need is  to be able to edit the contents of “ExistingContents” with my keyboard And then have session file it  away in the computer. In the past I've done this using <textarea>. I have tried for hours and hours and hours to figure out a way to Mix  <textarea> with <session>, but with no luck. If I keep them separate, then one works but the other doesn't. I'm Wondering, Maybe I'm using the wrong functions. Is there possibly a different function or set of functions that would do these two  operations without screwing each other up?

Share this post


Link to post
Share on other sites

There's no reason for things like that not to work together, it has to do with however you wrote the actual code.  If you build a form that submits to an ASP page, then you can get the submitted form data and save it in the session.  You can also use Javascript to get the data in a form and submit it to the server using ajax, and have ASP get the data and save it in the session that way.

Share this post


Link to post
Share on other sites

It's good to hear this should work. I have had  the most success with this code -

//--- try 4 -- -------------

ExistContents= “existing”;

<textarea cols=100 rows=20 >

<script>

localStorage.setItem("NewContents",<%Response.Write( ExistContents ) %> )

</script>

</textarea >

<br>

//------------------

 

<textarea> works fine, but then it prints out the localStorage command as though it were text, instead of Executing it. So then I tried this -

 

//--- try 5 -- -------------

ExistContents= “existing”;

<textarea cols=100 rows=20 >

<%Response.Write( <script> localStorage.setItem( "NewContents", "ExistContents" ) </script> ) %>

</textarea >

<br>

//------------------

. . . But that works no better.

Edited by crazyswede
grammer

Share this post


Link to post
Share on other sites

but then it prints out the localStorage command as though it were text, instead of Executing it.

Well, yeah, anything you put inside a textarea element is the contents for that textarea element.  If you want to execute that code put it somewhere else.  You also need to put quotes around the value you want to set.

You also still need to get the submitted value of that textarea.  I can tell you're not doing that because you haven't given it a name.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×