SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    May 2004
    Location
    houston
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Remove Sel Options, Replace them?

    I am a beginner at JS, and I am trying to get it to interact with PHP.. soo I've put together this code, but it doesn't seem to work quite right.... its pretty simple..

    Code:
    <form name="myForm">
    <select name="mySelect">
    <option>Some armor</option>
    <option>Shield</option>
    <option>Boots!</option>
    </select>
    <a href="#" onClick="seeWeapons();">See Weapons</a>
    <br>
    <a href="#" onClick="seeArmor();">See Armor</a>
    <script language="JavaScript">
    function seeWeapons() {
    var sel = document.myForm.mySelect;
    sel.options.length = 0;
    <?php
    $weapons = array("Dagger", "Sword", "Slingshot");
    for($i=0;$i<count($weapons);$i++) {
    echo "sel.options[$i] = new Option($weapons[$i], \"Blah\")";
    }
    ?>
    }
    </script>
    It should work, I've tried doing this as well in the for loop...

    Code:
    echo "sel.options[$i] = $weapons[$i]";
    still nothing, maybe I am trying to make the two interact incorrectly, any help on this subject would be wonderful, THank you very much. (i am looking around the forum for information pertaining to this as well.)

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    Netherlands
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you run this, it will be echoed like:

    sel.options[0] = new Option(test, "Blah");

    Try adding quotes around the string test. so:
    PHP Code:
    echo "sel.options[$i] = new Option(\"$weapons[$i]\", \"Blah\")"
    Personally, i do not like this functions, i'd rather do:
    Code:
    theOption = document.createElement("OPTION");
    theOption.value= "some value";
    theOption.innerHTML = "some text";
    document.getElementById("mySelect").appendChild(theOption);
    Little bit of this and a liitle bit of that should do the job

  3. #3
    SitePoint Zealot
    Join Date
    May 2004
    Location
    houston
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hehe I got it to work like I wanted it to.

    PHP Code:

    <html> 
    <head><title></title></head> 
    <script language="JavaScript"> 

    weapon = new Array() 
    armor = new Array() 
    <?php 
    $weapons 
    file("weapon.txt"); 
    $armor file("armor.txt"); 
    for(
    $i=0;$i<count($weapons);$i++) 
            { 
            
    $weapons[$i] = trim($weapons[$i]); 
            echo 
    "weapon[$i] = \"$weapons[$i]\"\n"
            } 
    for(
    $i=0;$i<count($armor);$i++) { 
            { 
            
    $armor[$i] = trim($armor[$i]); 
            echo 
    "armor[$i] = \"$armor[$i]\"\n"
            } 

    ?> 

    function seeWeapons() 
            { 
            clearAll(); 
            var sel = document.myForm.mySelect; 
            for (i=0; i<weapon.length; i++) 
                    { 
                    sel.options[sel.options.length] = new Option(weapon[i], weapon[i]); 
                    } 
            } 
    function seeArmor() { 
            clearAll() 
            var sel = document.myForm.mySelect; 
            for (i=0; i<armor.length; i++) { 
                    sel.options[sel.options.length] = new Option(armor[i], armor[i]); 
            } 

    function clearAll() { 
            var sel = document.myForm.mySelect; 
            sel.options.length = 0; 

    </script> 
    </head> 
    <body> 
    <form name="myForm" method="POST"> 
    <select name="mySelect"> 
    <option value=null>Select a Shop</option> 
    </select><br> 
    <a href="javascript:seeWeapons()">See Weapons</a><Br> 
    <a href="javascript:seeArmor()">See Armor</a><br> 
    </form> 
    </body> 
    </html>
    It works like a charm, you can look at it here:

    http://anger.roguemind.com/site/example.php

    Thanks for the help though.


Bookmarks

Posting Permissions

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