www.webdeveloper.com
Results 1 to 6 of 6

Thread: onChange based on database

  1. #1
    Join Date
    Jan 2010
    Location
    USA
    Posts
    4

    onChange based on database

    I am creating a form in which a user selects an event from a <select> menu and if the event is password protected, a code box will appear below.

    The problem is that the list is generated from the server on a mysql page. What I am trying to do is make the box appear only if it is password protected. My first idea was to set the value of the protected options to "protected" or something similar, but this won't work as I will lose the value it is supposed to be.

    Is there a way this can be done? Thanks!

  2. #2
    Join Date
    Aug 2007
    Location
    London
    Posts
    410
    Since you're already using Javascript you could have an object containing event details, an effective model. You could then just have an index as the value for each option and the javascript looks up the appropriate information in the model for the selected event.

    I think that's the best option, personally. You could also do it by assigning non-standard tags to the various options.

  3. #3
    Join Date
    Jan 2010
    Location
    USA
    Posts
    4
    I was considering the "non-standard tag" method by using class or id, but then I realized that I had no idea how to retrieve which option class or option id was selected.

    Any ideas for that method?

    Further, I build an array with the values that should be protected, but wasn't sure how I was going to go about checking to see if the value selected was on the list. Any ideas for this method?

    Thanks!

  4. #4
    Join Date
    Jan 2010
    Location
    USA
    Posts
    4
    After doing some searching, I came across in_array(), and I am going to try to use the generated array with the protected values and the in_array() function to check if the selected value is in the array.

  5. #5
    Join Date
    Jan 2010
    Location
    USA
    Posts
    4
    I just realized I can't edit because I forgot to add...

    I got the code from stackoverflow

    Code:
    function inArray(needle, haystack) {
        var length = haystack.length;
        for(var i = 0; i < length; i++) {
            if(haystack[i] == needle) return true;
        }
        return false;
    }

  6. #6
    Join Date
    Aug 2007
    Location
    London
    Posts
    410
    You could do something like the following:

    HTML Code:
    <html>
    <body>
    <script language="javascript" type="text/javascript">
    options = {
    	0: {"protected": true, "name": "Option 1"},
    	1: {"protected": false, "name": "Option 2"}
    };
    
    var myFunction = function(obj) {
    	if(options[obj.selectedIndex]["protected"]) {
    		alert("Protected!");
    	} else {
    		alert("Unprotected!");
    	}
    };
    </script>
    <select id="one" name="one" onchange="myFunction(this);">
    	<option>Option 1</option>
    	<option>Option 2</option>
    </select>
    </body>
    </html>
    That's the general principle at least.
    Last edited by blue-eye-labs; 01-23-2010 at 07:41 PM.

Thread Information

Users Browsing this Thread

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

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
  •  
HTML5 Development Center



Recent Articles