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
    Jun 2012
    Posts
    19
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Preferences ordered by JS in a form

    I would like to do that if my user choose in the first choice "A" he can't choose it in the second and last one. How can I do it with JS?
    Code:
                  First choice:
    		
    			<select id="pref1" name="pref1">
    					<option value="1">A</option>
    					<option value="1">B</option>
    					<option value="1">C</option>
    			</select>
    
    		Second:
    
    			<select id="preferencja2" name="preferencja2">
    					<option value="2">A</option>
    					<option value="2">B</option>
    					<option value="2">C</option>
    			</select>
    
    
    		Last:		
    
    			<select id="preferencja3" name="preferencja3">
    					<option value="3">A</option>
    					<option value="3">B</option>
    					<option value="3">C</option>
    			</select>
    Do you have any tutorial?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Code:
    <html>
    <head>
    </head>
    
    <body>
    <script type = "text/javascript">
    
    function NewList(selObj,newObj,addObj) {
    var selElem = document.getElementById(selObj);
    var selIndex = selElem.selectedIndex;
    var newElem = document.getElementById(newObj);
    var tmp = '';
    newElem.options.length = 0;
    
    for (var i=0; i<selElem.options.length; i++) {
    tmp = selElem.options[i].value;
    if (i != selIndex) { newElem.options[newElem.options.length] = new Option(tmp,tmp); }
    }
    }
    
    function show() {
    var message = "Your selections were:- ";
    if ((document.getElementById("SBox0").value != "Select A Fruit") && (document.getElementById("SBox1").value != "Select A Fruit") && (document.getElementById("SBox2").value != "Select A Fruit") ) {
    message += document.getElementById("Sbox0").value + " ";
    message += document.getElementById("Sbox1").value + " ";
    message += document.getElementById("Sbox2").value;
    }
    else {message = "You must make a selection from all three lists"}
    alert (message);
    }
    
    </script>
    
    FIRST CHOICE <select id="SBox0" onchange="NewList('SBox0','SBox1','AddedOptions')">
    <option value ="Select A Fruit">Select A Fruit </option>
    <option value="Apple">Apple</option>
    <option value="Orange">Orange</option>
    <option value="Mango">Mango</option>
    </select>
    
    SECOND CHOICE <select id="SBox1" onchange="NewList('SBox1','SBox2','')">
    <option value=""></option>
    </select>
     
    THIRD CHOICE <select id="SBox2">
    <option value=""></option>
    </select>
    
    &nbsp &nbsp <input type = "button" value = "Show Selections" onclick = "show()">
    </body>
    </html>
    You can of course add as many options as you wish to Sbox0.


    Newcastle and Chelsea have to play each other, and I don't think they'll both win that game. - Commentator Radio 5 Live
    Last edited by Philip M; 07-05-2012 at 06:51 PM. Reason: Improved

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    tobiasf (07-05-2012)

  • #3
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Seeing as all options for each are the same (ie, A, B, and C in the first have a value of 1; A, B, and C in the second have a value of 2; etc.) you could do it, but you'd have to get the text of the selectedIndex instead of the value.

    But this is obviously for homework. These forums are not for getting people to do your code, for you; they are to seek help after an effort has already been made. Do you have any JavaScript code that isn't working?


    UPDATE: Nevermind.
    Last edited by WolfShade; 07-05-2012 at 05:55 PM. Reason: Philip M beat me to it. :)
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by WolfShade View Post
    But this is obviously for homework.
    Is it? It is not obvious to me!

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New Coder
    Join Date
    Jun 2012
    Posts
    19
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks guys I have tried to do something for whole day with any success I'm learning from this forum very much and I hope I can soon help somebody as you do. It is great work

    I have problem with this script. I can't sent it to my database
    What can be wrong?

    choice.php
    PHP Code:
    <html>
    <
    head>
    </
    head>

    <
    body>
    <
    form id="choice" action="index.php" method="post">
    <
    script type "text/javascript">
    function 
    NewList(selObj,newObj,addObj) {
    var 
    selElem document.getElementById(selObj);
    var 
    selIndex selElem.selectedIndex;
    var 
    newElem document.getElementById(newObj);
    var 
    tmp '';
    newElem.options.length 0;

    for (var 
    i=0i<selElem.options.lengthi++) {
    tmp selElem.options[i].value;
    if (
    != selIndex) { newElem.options[newElem.options.length] = new Option(tmp,tmp); }
    }
    }

    function 
    show() {
    var 
    message "Your selections were:- ";
    if ((
    document.getElementById("SBox0").value != "Select A Fruit") && (document.getElementById("SBox1").value != "Select A Fruit") && (document.getElementById("SBox2").value != "Select A Fruit") ) {
    message += document.getElementById("Sbox0").value " ";
    message += document.getElementById("Sbox1").value " ";
    message += document.getElementById("Sbox2").value;
    }
    else {
    message "You must make a selection from all three lists"}
    alert (message);
    }
    </script>
    FIRST CHOICE <select id="SBox0" onchange="NewList('SBox0','SBox1','AddedOptions')">
    <option value ="Select A Fruit">Select A Fruit </option>
    <option value="Apple">Apple</option>
    <option value="Orange">Orange</option>
    <option value="Mango">Mango</option>
    </select>

    SECOND CHOICE <select id="SBox1" onchange="NewList('SBox1','SBox2','')">
    <option value=""></option>
    </select>
     
    THIRD CHOICE <select id="SBox2">
    <option value=""></option>
    </select>

    &nbsp &nbsp <input type = "submit" value = "Submit" onclick = "show()">
    </body>
    </html> 
    index.php
    PHP Code:
    <?php 

    $SBox0 
    $_POST['SBox0'];
    $SBox1 $_POST['SBox1'];
    $SBox2 $_POST['SBox2'];

            
    if(
    $SBox0 and $SBox1 and $SBox2  ) { 
         
      
        
    $connection = @mysql_connect('****''********''*********'
        or die(
    'error');
        
    mysql_query("SET NAMES 'utf8'"); 
        
    $db = @mysql_select_db('*****_choice'$connection
        or die(
    'error'); 
         
        
        
    $ins = @mysql_query("INSERT INTO choice SET SBox0='$SBox0', SBox1='$SBox1', Sbox2='$SBox2'"); 
         
        if(
    $ins) echo "Thanks"
        else echo 
    "<strong>Sorry but we have problem</strong>"
         
        
    mysql_close($connection); 

    ?>
    After I click submit - index.php is blank

    Ok I have done it by my own Thanks guys one more time!!!
    Last edited by tobiasf; 07-05-2012 at 08:17 PM.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by tobiasf View Post
    Ok I have done it by my own Thanks guys one more time!!!
    So tell us how you solved your problem. Then others can perhaps learn.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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