global variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Soha

    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

  • Randy Webb

    #2
    Re: global variables

    Soha wrote:[color=blue]
    > 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>[/color]

    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.
    [color=blue]
    > logically this code must work but i don't know why it dosen't.[/color]

    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

    Comment

    • Soha

      #3
      Re: global variables

      > 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>

      Comment

      • Soha

        #4
        Re: global variables

        > 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 :[color=blue]
        > update the hidden field[/color]

        And thank you

        Comment

        • Randy Webb

          #5
          Re: global variables

          Soha wrote:[color=blue][color=green]
          >>It works, it just doesn't do what you want it to do.[/color]
          >
          > 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 :
          >[color=green]
          >>update the hidden field[/color]
          >
          >
          > And thank you[/color]


          <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

          Comment

          • Richard Cornford

            #6
            Re: global variables

            Soha wrote:[color=blue][color=green]
            >> It works, it just doesn't do what you want it to do.[/color]
            > I know that it doesn't do what i want it to do that's
            > why i post this problem[/color]
            <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.


            Comment

            • Soha

              #7
              Re: global variables

              Thank u so much for your help. now the code works the same way i wanted

              Comment

              Working...