SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Addict say's Avatar
    Join Date
    Sep 2003
    Location
    At work
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Populating mysql data to empty selectlist

    HI, I'm trying to dynamically retrieve data from database and stuff all these data into an empty select list thru javascript. But I can't get it to work

    PHP Code:
    while( $row mysql_fetch_array$sql ) )
    {
           
    $getChildren .= 'var newCElem = document.createElement("OPTION");';
           
    $getChildren .= 'newCElem.value = "' $row['id'] . '";';
           
    $getChildren .= 'newCElem.text  = "' $row['label'] . '";';
           
    $getChildren .= 'allPaths.add(newCElem);';

    $getChildren will be spit out to my javascript code part where I've place <%getChildren%>. When I try to manually type in the output into the javascript code, it works but not when dynamically.

    anyone?

  2. #2
    SitePoint Addict say's Avatar
    Join Date
    Sep 2003
    Location
    At work
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, guess what? I got the result. Here's what I did for those who are looking for the same thing.

    First, I generate a set of Javascript array using the data in my database and it gave me this:
    Code:
    ar[0] = new Array( );
    ar[0][0] = new Option( "Implementing Secure", "1" );
    
    ar[1] = new Array( );
    ar[1][0] = new Option( "myfolder", "2" );
    ar[1][1] = new Option( "inside myfolder", "6" );
    
    ar[2] = new Array( );
    ar[2][0] = new Option( "another folder", "3" );
    
    ar[3] = new Array( );
    ar[3][0] = new Option( "hot enough?", "4" );
    ar[3][1] = new Option( "inside hot enough", "5" );
    ar[3][2] = new Option( "inside again", "7" );

    Then I use the below code in JS to add option into the empty list dynamically. folderID is an int where the user clicks on and calls the above specific array.
    Code:
    for( i=0; i<ar[folderID].length; i++ )
       {
       	  allPaths[i] = new Option(ar[folderID][i].text,ar[folderID][i].value)
       }
    Sorry if I did not explain well as I've got to run for another problem


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
  •