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 6 of 6
  1. #1
    New Coder
    Join Date
    Mar 2004
    Posts
    95
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Select box help?

    Hi,

    I want to stop my form from submitting if a select box value has not been chosen. When I use this code it works fine
    PHP Code:
    <script language="JavaScript" type="text/JavaScript">
    function 
    checkFormform1 ) {
            if( !
    document.form1.select.options[document.form1.select1.selectedIndex].value ) {
                    var 
    answer window.alert"You did not select an option." );
                    if( 
    answer ) {
                            return 
    true;
                    } else {
                            return 
    false;
                    } 
            } 
            return 
    true;

    </script> 
    but my select boxes are an array and instead of being called "select1" it is called "option[1]". If I subbstitute "option[1]" for "select1" in the javascript it no longer works. Can anyone help me with this?

  • #2
    New Coder
    Join Date
    Apr 2007
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ur question is not clear...
    plz give a clear picture of it once agian

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    41 8' 52" N -95 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can we see the rest of the form?
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #4
    New Coder
    Join Date
    Mar 2004
    Posts
    95
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by venkatmallu.net View Post
    ur question is not clear...
    plz give a clear picture of it once agian
    Sorry, I will try to explain better. Part of my form is this:-
    PHP Code:
    <form action="shop.php" name="form1" method="post">
    <
    select id="option[1]" name="option[1]" class="select">
    <
    option value="">SELECT Colour</option>
    <
    option value="7">Grey</option>
    <
    option value="8">Red</option>
    </
    select>
    </
    form
    Note the name and id of the select. Now I want to stop the form being submitted if a Colour is not selected. My javascript is this:-
    PHP Code:
    <script language="JavaScript" type="text/JavaScript">
    function 
    checkFormform1 ) {

            if( !
    document.form1.option[1].options[document.form1.option[1].selectedIndex].value ) {
                    var 
    answer window.alert"You did not select an option." );
                    if( 
    answer ) {
                            return 
    true;
                    } else {
                            return 
    false;
                    } 
            } 
            return 
    true;

    </script> 
    But that doesn't work. If I change the name of the select to something different like "select1" instead of "option[1]" in both lots of code it does work. But the select boxes are made using php and there may be more than one so they are named as an array. Any ideas how to make my javascript work using the existing select name?

    I hope that explains my problem better.

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Code:
    document.form1.elements["option[1]"]
    See Javascript Square Bracket notation in my sig for more info.

  • #6
    New Coder
    Join Date
    Mar 2004
    Posts
    95
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv View Post
    Code:
    document.form1.elements["option[1]"]
    See Javascript Square Bracket notation in my sig for more info.
    Thanks ... perfect


  •  

    Posting Permissions

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