Populate a select box from another select box

Discussion in 'PHP' started by zakula11, Jun 25, 2006.

  1. #1
    I have two select boxes. I need so that: when an option on one select box is selected, PHP opens a file (based on the value of the option selected). The information inside the file needs to be read and used to create options in the second select box.

    I believe I know how to read the file and then possibly echo the options (if the php code is inside a javascript function), but the problem is that the filename is based on the selected option in the first select box. I'm not sure how to pass the value of that variable into the php code, so that PHP knows what file to open. Submitting a form is not an option because then it redirects the page.

    To make this easier to understand, here is the actual page I am dealing with: http://uglyduck.homeip.net/musicTest.php


    There is a select box of saved playlists. The names of the playlists are the same names as the filenames. In the file, each line contains a filename to a song. I want so that when you select a playlist and click "Load", that it will create an option for each listed song in the "current playlist" select box.

    I have been suggested to use XMLHttpRequest from another forum. I have tried this but can't seem to be able to get it to work. If this is the correct way to do this, helping me with this exact case or similar code examples would be nice.

    Thanks to anyone that can help me with this issue.
     
    zakula11, Jun 25, 2006 IP
  2. zak

    zak Peon

    Messages:
    175
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    pass it through the url as a get variable
     
    zak, Jun 25, 2006 IP
  3. zakula11

    zakula11 Guest

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I've tried that and nothing happened... Maybe you can help me fix it? Here is what I had...

    Here is my javascript function called when "Load" button is clicked
    function loadPlaylist()
    {
      var name = saveList.options[saveList.selectedIndex].value;
      http = new ActiveXObject("Microsoft.XMLHTTP");
      http.onreadystatechange=httpChange();
      http.open('GET', "loadPlaylist.php?id=" + name, true);
      http.send(null);
      
      
    }
    
    function httpChange()
    {
      if(http.readyState==4)
      {
        var response = http.responseText;
        document.getElementById('playList').innerHTML = response;
      }
    }
    HTML:

    Here is loadPlaylist.php
    <?php
    
    $name = $_GET['id'];
    
    $File = "ftp://uglyduck.homeip.net/Music/" . $name . ".txt";
    $Handle = fopen($File, 'r');
    
    
      $Data = fgets($Handle);
      $Data = substr($Data, 7);
      $Data2 = substr($Data, 4);
    
      $data3 = '<option value="/Music/50 Cent - Best Friend.mp3">50 Cent - Best Friend';
    
    
    fclose($Handle);
    
    echo $data3
    
    
    ?>
    PHP:
    Until I get this to work I just have $data3 as a static option to make sure its formatted right and all.

    With this code, I get a "Type mismatch" error when Load is pressed.
    if i change the line:
    http.onreadystatechange=httpChange();
    HTML:
    to:
    http.onreadystatechange=httpChange;
    HTML:
    I dont get the error, but nothing happens at all


    Thanks
     
    zakula11, Jun 25, 2006 IP
  4. smo

    smo Well-Known Member

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #4
    smo, Jun 25, 2006 IP
  5. zakula11

    zakula11 Guest

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That's not exactly what I'm trying to do. I already accomplish that through javascript from songlist to current playlist.

    I need to OPEN A SERVER-SIDE FILE with the filename being the selected option in the "saved playlist" select box

    this file has songnames on each line of a text file

    they need to be read in and create options with those song names in another select box.
     
    zakula11, Jun 26, 2006 IP
  6. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #6
    Use flash as a media player, and javascript to communicate the current song-to-play to the flash player.
     
    ccoonen, Jun 28, 2006 IP