473,549 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

global variables

I'm a beginner in using javascript and i need a reply so urgent;
i have a problem in using javascript with asp the problem is that i
want to define a global variable and then change the value of it after
clicking on a link which activate a function that changes the global
variable. this variable i use as the value of a hidden field in a form.
my problem is that the global variable dosen't change.
this is the code i used to do this:

//the function which shows a layer and changes the variable
<script>
var month_na=0;//global variable
function show_layer(mont h_no)
{
month_na=month_ no;
document.getEle mentById("layer n").style.visib ility="visible" ;
return month_na;
}
</script>

//the link code <%=rs_trans_mo( 0)%> is asp code and is evaluated to 3
<a onclick="show_l ayer(<%=rs_tran s_mo(0)%>);" style="color:
#0000FF;cursor: hand;">+</a>

//the form that uses the global variable
<td id="layern" style="visibili ty:hidden;" nowrap>
<form method="POST" action="insert_ trans.asp">
day: <input type="text" name="day_no" size="20">
<p>main rate:<input type="text" name="main_rate " size="20"></p>
<p>paid rate:<input type="text" name="paid_rate " size="20"></p>
<p><input type="submit" value="Submit" name="B1"></p>
<script language=javasc ript>
//the hidden field used to send the global variable to the
insert_trans.as p page
if (month_na!=0)
{document.write ('<input type="hidden" name="month_no"
value="'+month_ na+'">');}
</script>
<input type="hidden" name="year_no" value="<%=year_ no%>">
</form>
</td>

logically this code must work but i don't know why it dosen't. plz try
to reply ass soon as possible coz i need this very urgently.
thank u all

Jul 23 '05 #1
6 3930
Soha wrote:
I'm a beginner in using javascript and i need a reply so urgent;
i have a problem in using javascript with asp the problem is that i
want to define a global variable and then change the value of it after
clicking on a link which activate a function that changes the global
variable. this variable i use as the value of a hidden field in a form.
my problem is that the global variable dosen't change.
this is the code i used to do this:

//the function which shows a layer and changes the variable
<script>
var month_na=0;//global variable
function show_layer(mont h_no)
{
month_na=month_ no;
document.getEle mentById("layer n").style.visib ility="visible" ;
return month_na;
}
</script>

//the link code <%=rs_trans_mo( 0)%> is asp code and is evaluated to 3
<a onclick="show_l ayer(<%=rs_tran s_mo(0)%>);" style="color:
#0000FF;cursor: hand;">+</a>

//the form that uses the global variable
<td id="layern" style="visibili ty:hidden;" nowrap>
<form method="POST" action="insert_ trans.asp">
day: <input type="text" name="day_no" size="20">
<p>main rate:<input type="text" name="main_rate " size="20"></p>
<p>paid rate:<input type="text" name="paid_rate " size="20"></p>
<p><input type="submit" value="Submit" name="B1"></p>
<script language=javasc ript>
//the hidden field used to send the global variable to the
insert_trans.as p page
if (month_na!=0)
{document.write ('<input type="hidden" name="month_no"
value="'+month_ na+'">');}
</script>
<input type="hidden" name="year_no" value="<%=year_ no%>">
</form>
</td>
If you make month_no a visible field, you will see that it is never
changed because nothing in your code changes it. What you need instead
of a global variable is to simply update the hidden field.

In the future, please do not post your ASP code, its irrelevant. When
posting code, post the code that is transmitted to the browser.
logically this code must work but i don't know why it dosen't.


It works, it just doesn't do what you want it to do.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #2
> It works, it just doesn't do what you want it to do.
I know that it dosen't do what i want that's why i post this code.
I just want to know what u mean with update the hidden field?

and sorry for posting the asp code and here is the code that is
transmited to the browser:

//the function which shows a layer and changes the variable
<script>
var month_na=0;
function show_layer(mont h_no)
{
month_na=month_ no;
document.getEle mentById("layer n").style.visib ility="visible" ;
return month_na;
}
</script>
//the link code
<a onclick="show_l ayer(3);" style="color: #0000FF;cursor: hand;">+</a>
//the form that uses the global variable and it is shown only when the
user click on the link
<td id="layern" style="visibili ty:hidden;" nowrap>
<form method="POST" action="insert_ trans.asp">
day: <input type="text" name="day_no" size="20">
<p>main rate:<input type="text" name="main_rate " size="20"></p>
<p>paid rate:<input type="text" name="paid_rate " size="20"></p>
<p><input type="submit" value="Submit" name="B1"></p>
//the hidden field used to send the global variable to the
insert_trans.as p page
<script language=javasc ript>
if (month_na!=0){
document.write ('<input type="hidden" name="month_no"
value="'+month_ na+'">');}</script>
<input type="hidden" name="year_no" value="2000">
</form>
</td>

Jul 23 '05 #3
> It works, it just doesn't do what you want it to do.
I know that it doesn't do what i want it to do that's why i post this
problem
Sorry for the asp code and here is the code again without the asp code

//the function which shows a layer and changes the variable
<script>
var month_na=0;//global variable
function show_layer(mont h_no)
{
month_na=month_ no;
document.getEle mentById("layer n").style.visib ility="visible" *;
return month_na;
}
</script>
//the link code
<a onclick="show_l ayer(3);" style="color: #0000FF;cursor: hand;">+</a>
//the form that uses the global variable
<td id="layern" style="visibili ty:hidden;" nowrap>
<form method="POST" action="insert_ trans.asp">
day: <input type="text" name="day_no" size="20">
<p>main rate:<input type="text" name="main_rate " size="20"></p>
<p>paid rate:<input type="text" name="paid_rate " size="20"></p>
<p><input type="submit" value="Submit" name="B1"></p>
<script language=javasc ript>
//the hidden field used to send the global variable to the
insert_trans.as p page
if (month_na!=0)
{document.write ('<input type="hidden" name="month_no"
value="'+month_ na+'">');}
</script>
<input type="hidden" name="year_no" value="2000">
</form>
</td>

i just want to know what you mean with :
update the hidden field


And thank you

Jul 23 '05 #4
Soha wrote:
It works, it just doesn't do what you want it to do.


I know that it doesn't do what i want it to do that's why i post this
problem
Sorry for the asp code and here is the code again without the asp code

//the function which shows a layer and changes the variable
<script>
var month_na=0;//global variable
function show_layer(mont h_no)
{
month_na=month_ no;
document.getEle mentById("layer n").style.visib ility="visible" *;
return month_na;
}
</script>
//the link code
<a onclick="show_l ayer(3);" style="color: #0000FF;cursor: hand;">+</a>
//the form that uses the global variable
<td id="layern" style="visibili ty:hidden;" nowrap>
<form method="POST" action="insert_ trans.asp">
day: <input type="text" name="day_no" size="20">
<p>main rate:<input type="text" name="main_rate " size="20"></p>
<p>paid rate:<input type="text" name="paid_rate " size="20"></p>
<p><input type="submit" value="Submit" name="B1"></p>
<script language=javasc ript>
//the hidden field used to send the global variable to the
insert_trans.as p page
if (month_na!=0)
{document.write ('<input type="hidden" name="month_no"
value="'+month_ na+'">');}
</script>
<input type="hidden" name="year_no" value="2000">
</form>
</td>

i just want to know what you mean with :
update the hidden field

And thank you

<script type="text/javascript">
function show_layer(mont h_no)
{
document.getEle mentById("layer n").style.visib ility="visible" *;
//updates the hidden field:
document.forms['myForm'].elements['month_no'].value = month_no;
}
</script>
//the link code
<a onclick="show_l ayer(3);" style="color: #0000FF;cursor: hand;">+</a>
[note]A link that goes nowhere (not even an href attribute) is invalid
[note]HTML but it also makes your pages inaccessible to non-JS people.

<td id="layern" style="visibili ty:hidden;" nowrap>
<form method="POST" action="insert_ trans.asp" name="myForm">
day: <input type="text" name="day_no" size="20">
<p>main rate:<input type="text" name="main_rate " size="20"></p>
<p>paid rate:<input type="text" name="paid_rate " size="20"></p>
<p><input type="submit" value="Submit" name="B1"></p>

[note]No need to dynamically insert the hidden field, just hard code it.

<input type="hidden" name="month_no" value="1">

<input type="hidden" name="year_no" value="2000">
</form>
</td>

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #5
Soha wrote:
It works, it just doesn't do what you want it to do.

I know that it doesn't do what i want it to do that's
why i post this problem

<snip>

The point is that when what code does doesn't correspond with what it is
intended to do the intention cannot be deduced form the code. You have
to actually say what it is that would qualify as "working", because the
computer is going to do exactly what you programmed it to do regardless
of intentions and so code, as such, always "works".

Richard.
Jul 23 '05 #6
Thank u so much for your help. now the code works the same way i wanted

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
17838
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can overwrite another copies global variable? I know that you could overwrite a value in a global variable from a function, but you could also do that if...
4
24163
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); function loadUrlVariables() { varString = location.search;
12
5859
by: David WOO | last post by:
Hi, I am a newbie on C++, I need to define some global variables which should be accessible to most classes. In the mean time, I don't won't the global variables be modified freely at most of these classes. I know there is a pattern called singleton can more or less do such a trick. I am wondering is this the best way to do it (regarding...
2
5174
by: Bryan Parkoff | last post by:
….I would like to know which is the best optimization to use global variable or global struct. I always tell C/C++ Compiler to turn on optimization. ….I use underscore between first name and second name for better readable. After optimization, global variables might be misaligned because each global variables must be converted to 32 bits,...
17
5604
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm constantly running tests on imperfect code. On of the cumbersome jobs encountered is reassigning global vars their values after a close encounter with...
33
3002
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have been for years. If the machine has memory to spare and windows can use it - I'm thinking "Why not?" I was wondering what some of you have to say...
9
8627
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang Su Gatlin, casual mention was made about using static variables as an alternative to using global variables. This caused me to think of the...
5
11802
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global $MYVAR, $a; ?>
1
29323
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this...
112
5376
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions that may print some messages. foo(...) { if (!silent)
0
7526
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7723
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7483
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5092
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3504
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
771
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.