Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Posts
    362
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Fetch form value

    I am trying to get a form value on an action page using JavaScript to alert the value.

    The below doesnt alert anything after I hit the submit button in the one.html page:

    one.html
    Code:
    <form action="two.html" method="get" id="myform">
    <input type="text" name="city">
    <input type="submit">
    two.html
    Code:
    <html>
    <head>
    <script>
    alert(document.myform.city.value);
    </script>
    </head>
    <body>
    </body>
    </html>
    Please advise.

  • #2
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    Quote Originally Posted by florida View Post
    I am trying to get a form value on an action page using JavaScript to alert the value.

    The below doesnt alert anything after I hit the submit button in the one.html page:

    one.html
    Code:
    <form action="two.html" method="get" id="myform" name="myform">
    <input type="text" name="city">
    <input type="submit">
    two.html
    Code:
    <html>
    <head>
    <script>
    alert(document.myform.city.value);
    </script>
    </head>
    <body>
    </body>
    </html>
    Please advise.
    try that. I beleive the myform in "alert(document.myform.city.value)" , refers to the name attrib of the form not the id

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Posts
    362
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Still not working.
    Please advise.

  • #4
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,899
    Thanks
    15
    Thanked 226 Times in 226 Posts
    Code:
    <form action="two.html" method="get" id="myform" name="myform">
    
    <input type="text" name="city">
    
    <input type="submit" onclick="myval();">
    
    </form>
    
    </div>
    
    <script>
    
    function myval()
    
    {
    
    alert(document.myform.city.value);
    
    }
    
    </script>

  • #5
    Regular Coder
    Join Date
    Jul 2002
    Posts
    362
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks that works on one.html but need the alert to show on the action which would be two.html.

    Please advise.

  • #6
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    one.html
    Code:
    <form action="/two.html" method="get" id="myform">
    <input type="text" name="city">
    <input type="submit">
    </FORM>
    two.html
    Code:
    <html>
    <head>
    <script>
    alert(location.search);
    </script>
    </head>
    <body>
    </body>
    </html>
    how about this? you may have to parse the resulting string ( http://www.eggheadcafe.com/articles/20020107.asp) , also oddly the data wasnt posting into the url without the / in front of two.html when i was trying.

  • #7
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,899
    Thanks
    15
    Thanked 226 Times in 226 Posts
    You need to have two forms with two different names and the closing tags.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    If you are trying to pass a value from one page to another you must use window.name, a query string or a cookie. This subject has been covered very many times in this forum.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •