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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Submit to make active variable

    Hi! New here, but was hoping to get this figured out in the next day or two.

    I'm working on a project where the User enters a Zip code and on Submit, the Zip Code is available under the variable "zip".

    I've been trying a lot of things, so either this coding is close or may not make sense at all. I'm using it to get the already stored "zip" from Yahoo's simpleWeather. At the bottom of this coding, the last line reads that I'm assigning "zip" the function "zipfx" in hope that "zip" will run the function which grabs the zip code entered from the form. I appreciate any help ASAP!

    My current HTML:
    <form name="zipSearch" action="" method="post">
    <fieldset>
    <input type="text" name="zipEntry" placeholder="Enter Zip Code" value=""/>
    <input type="submit" name="zipSubmit" value="Search" onClick="zipfx()"/>
    </fieldset>
    </form>

    My current JS:
    function zipfx(){

    var zipd= document.forms["zipSearch"]["zipEntry"].value;
    return zipd;
    }

    var zip = zipfx();




    ---

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Code:
    var zip = zipfx();
    This executes the function zipfx and assigns the return result to the variable zip. But it runs straight-away, before the form exists, so it returns an error as it cannot refer to forms['zipSearch'].

    Code:
    var zip = zipfx;
    Assigns the function to the variable, so that it could be executed later by calling

    Code:
    zip();
    FYI It is preferable and more modern to use IDs rather than names. In particular, form-names are deprecated/discouraged.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Maybe like this ...

    Code:
     
    <!doctype html>
    <html>
    <head>
    <style>  
    </style>
    <script>
    function zipfx(frm){  
     var zipid = frm.zipEntry.value;
     alert(zipid);
     return false;// prevents submit, return true to allow submit
    }
    </script>
    </head>
    <body>
     <form action="" method="post" onsubmit="return zipfx(this)">
      <fieldset>
       <input type="text" name="zipEntry" placeholder="Enter Zip Code" value=""/>
       <input type="submit" value="Search" />
      </fieldset>
     </form>
    </body>
    </html>


  •  

    Tags for this Thread

    Posting Permissions

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