Capturing value entered in textfield of form and using in javascript code

Discussion in 'JavaScript and AJAX' started by saturn, Jun 2, 2009.

  1. saturn

    saturn New Member

    Joined:
    Jun 2, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Hello!

    I am just a beginnerin javascript,so pls need your help!

    I am making a website using html.I need to capture a value from a textfield of one form in a page and use the value in an if-else loop in javascript in another page.
    How to capture the value that would be given by any user and use the value to run the loop to display certain information to the user according to the value? And where should I put the script in the head section or the body section? :confused:

    I have made the form as-

    <form action="tariff2.html">
    <center><b>Connected load:
    <input type="text" name="load" size="30"></b></center>
    <center><br><br><br>
    <input type="submit" name="Submit" value=" Submit "
    onKeyPress="return submitenter(this,event)"> &nbsp;&nbsp;&nbsp;&nbsp;
    <input type="reset" name="Reset" value=" Reset "></center>

    And the script as-

    <script type="text/javascript" language="javascript">
    var ld = load.value;
    if (ld<10)
    {
    document.write("<b>Consumer's Category: Domestic-A</b>");
    }
    else if (ld>10 && ld<20)
    {
    document.write("<b>Consumer's Category: Domestic-B</b>");
    }
    else
    {
    document.write("<b>Consumer's Category: Commercial</b>");
    }

    Any help would be appreciated
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    You can post via GET and access the query string to get the value in the next page.
     
  3. sriram225

    sriram225 New Member

    Joined:
    May 22, 2009
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Scripts in <head>
    Scripts to be executed when they are called, or when an event is triggered, go in the head section.

    If you place a script in the head section, you will ensure that the script is loaded before anyone uses it.

    Scripts in <body>

    Scripts to be executed when the page loads go in the body section.

    Code:
    <html>
    <head>
    <script type="text/javascript">
    ....
    </script>
    </head>
    <body>
    <script type="text/javascript">
    ....
    </script>
    </body>


    I have Modified ur Code as following save this in notepad as filename with .html extension and open it works!!!!

    Code:
    <html>
    <body>
    <script type="text/javascript" language="javascript">
    
    function cload(val)
    {
     var ld =val;
    if (ld<10)
     {
      document.write("<b>Consumer's Category: Domestic-A</b>");
     }
     else if (ld>10 && ld<20)
     {
      document.write("<b>Consumer's Category: Domestic-B</b>");
     }
     else
     {
      document.write("<b>Consumer's Category: Commercial</b>");
     }
    }
    </script>
    <form action="tariff2.html">
    <center><b>Connected load:
    <input type="text" name="load" size="30"></b></center>
    <center><br><br><br>
    <input type="button" name="Submit" value="Submit" onclick=cload(load.value)> 
    &nbsp;&nbsp;&nbsp;&nbsp;
    <input type="reset" name="Reset" value=" Reset "></center>
    </body>
    </html>
     
  4. sriram225

    sriram225 New Member

    Joined:
    May 22, 2009
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    You can post via GET and access the query string to get the value in the next page. 
    Yeah ,use attribute method="get" in form and acces that value using name in nxt page
     
  5. sriram225

    sriram225 New Member

    Joined:
    May 22, 2009
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Yeah ,use attribute method="get" in form and acces that value using name in nxt page
     
  6. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    (1) Why do you repeat your posts ?! :mad:

    (2) And the above quote is of pradeep, why did you change it to "Originally Posted by sriram225" ?? :mad:

    (3) Would you care to explain what your html code does ? I don't see anything "working" on my FF.
     
  7. sriram225

    sriram225 New Member

    Joined:
    May 22, 2009
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Code:
     (1) Why do you repeat your posts ?!
    
    (2) And the above quote is of pradeep, why did you change it to "Originally Posted by sriram225" ??
    
    (3) Would you care to explain what your html code does ? I don't see anything "working" on my FF. 

    Lol....I thought to edit my reply, acidentally made that replies.....Cool KID
     
  8. saturn

    saturn New Member

    Joined:
    Jun 2, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice