www.webdeveloper.com
Results 1 to 3 of 3

Thread: Javascript onchange pass hidden field

  1. #1
    Join Date
    Mar 2014
    Posts
    2

    Javascript onchange pass hidden field

    Hi, I do not know enough javascript to solve my problem, and I was hoping someone could help.

    I have a javascript jump menu that allows the user to select a language. They are then sent to whatever url corresponds to their language choice. I would like to include a couple of hidden fields that get sent with their response to help identify them on the new page, is this possible?

    If so, can someone show me how/where to put that code? Thanks so much!

    Here is my code:

    Code:
    <script language="JavaScript">function selecturl(s) 
    {
         var gourl = s.options[s.selectedIndex].value;
              if ((gourl != null) && (gourl != "") )
              {
                   window.top.location.href = gourl;
              }
    }
    </script>
    HTML Code:
    <form>
         <select name="Destination" onchange="selecturl(Destination)">
              <label for="Destination">Choose Language</label>
              <option value="">Choose Language</option>
              <option value="https://test.mywebsite.com/result.php?lng=English">English</option>
              <option value="https://test.mywebsite.com/result.php?lng=Spanish">Spanish</option>
         </select>
         <input type="hidden" name="username" value="testusername" />
         <input type="hidden" name="password" value="testpassword" />
    </form>

  2. #2
    Join Date
    Mar 2011
    Posts
    1,141
    You might use something like:
    Code:
    <script type="text/javascript">
    
    function selecturl(s) {
         var gourl = s.options[s.selectedIndex].value;
              if ((gourl != null) && (gourl != "") ) {
                   s.form.submit();
              }
       return false;
     } // end selecturl()
    
    </script>
    <form method="post" action="https://test.mywebsite.com/result.php">
         <label for="lng">Choose Language</label>
         <select name="lng" onchange="selecturl(this);">
              <option value="">Choose Language</option>
              <option value="English">English</option>
              <option value="Spanish">Spanish</option>
         </select>
         <input type="hidden" name="username" value="testusername" />
         <input type="hidden" name="password" value="testpassword" />
    <noscript>
         <input type="submit" value="Select" />
    </noscript>
    </form>
    This version passes all of the data and allows users running without JavaScript to use it as well.

    A word of caution: It's not a good idea to embed user credentials in an HTML form. It's too easily intercepted. It's also bad to pass that information in a query string, which is why I set the <form> 'method' to "post". Do some reading on website user authentication and security before you go much further. Good luck!
    Rick Trethewey
    Rainbo Design

  3. #3
    Join Date
    Mar 2014
    Posts
    2
    Thanks Rick, I appreciate your input. I think you are right about the security risks, I think I will come up with another way to handle this, but thanks so much for showing me how. This may come in handy on a future project that is not dealing with login credentials.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles