#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Posts
    3
    Rep Power
    0

    To all php/javascript pro's or guru's


    Hey Guys,
    I'm working on a php script that collects user information with an HTML <form>. I am not too swift with JavaScript(as mentioned in other posts), so if im out of line let me know. There are two places where i need to use JavaScript on this <form>.

    1) this piece of code shown below pulls data from a MySQL database and aranges it in category tree structure in a <select> box. The Problem is that the <select> box does not populate unless the page is refreshed. How can I adapt javascript with this php code to get it to load without having to refresh the page? the mysql database structure can be found at the end of this post.
    PHP Code:
    <?php
    // thanks to 'dustbuster', this works!
    function categories($parent,$parentstring){
        
    $query="SELECT * FROM categories where parent = '$parent'";
        
    $result=mysql_query($query);
         if (
    mysql_num_rows($result)>0){
             while (
    $row=mysql_fetch_assoc($result)){
                 
    $tempparentstring=$parentstring;
                 
    $tempparentstring=$tempparentstring." >> ".$row['Name'];
                 echo 
    "<option value=\"".$row['ID']."\">";
                 echo 
    $tempparentstring;
                 echo 
    "</option>\n";
                 
    categories($row['ID'],$tempparentstring);
             }
         }
    }
    //------------------
    require ("connect.php"); 
    echo 
    "<select name=\"Categories\">\n";
    $query="SELECT * FROM categories where parent IS NULL";
    $result=mysql_query($query);
    while (
    $row=mysql_fetch_assoc($result)){
        echo 
    "<option value=\"".$row['ID']."\">";
        echo 
    $row['Name'];
        echo 
    "</option>\n";
        
    categories($row['ID'],$row['Name']);
    }
    echo 
    "</select>\n";
    if (!
    mysql_select_db($databaseName$connection))
            
    showerror();

    // run it
    categories("NULL"""); 
    ?>
    2) Further down on the <form>, i need to populate another <select> box with a set of prices, that is bassed on the selection of the first <select> box(the above category <select> box).
    How can I populate this second <select> box bassed on the selection of the first, without refreashing the page?

    I know that the concept of this has proabally been explained and discussed many times on forums and in articles and tutorials found thru google, but.. My problem is I don't know enough javascript yet to manipulate other code to apply it to my script/application. I really would appreicate help from anyone who would help me get this working right. From what i have seen to me this all looks pretty complicated... Thanks alot in advance for any help!

    -- Database Structure for Categories --
    Table: categories
    ID | Name | Parent
    ---------------------------
    1 | Cars | NULL
    2 | Ford | 3
    3 | Coupe | 1
    4 | Constrution | NULL
    5 | Equipment | 4
    6 | Concrete | 5
    7 | Boom | 6
    8 | Hatchback | 1
    -----------------------------

    if this was the database of categories, this is what the output would look like in the <select> box.. (each line being a different option)

    Construction
    Construction > Equipment
    Construction > Equipment > Concrete
    Construction > Equipment > Concrete > Boom Pumps
    Cars
    Cars > Coupe
    Cars > Coupe > Ford
    Cars > Hatchback

    Thanks Again!
  2. #2
  3. WAR => Employment
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Location
    Portugal
    Posts
    2
    Rep Power
    0

    Lightbulb


    You can use Javascript / DHTML to do the trick.

    1. Build an array for each select option you want (with each sub-arrays), check the eval() function.
    2. Then implement an onchange event on the main select tag. This event should be a function that refills the select option with it's sub-options (a good idea is to place a go back option).

    Hope this will be helpfull.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Posts
    3
    Rep Power
    0
    hey thanks for the reply!
    I figured out how i could modify my php script so that it loads without having to refreah the page.. below is the modifications
    I didn't have to call the function agian and i needed to move the !mysql_select_db line from the bottom to the below the connect.php include.
    PHP Code:
    <?php
    // thanks to 'dustbuster', this works!
    function categories($parent,$parentstring){
        
    $query="SELECT * FROM categories where parent = '$parent'";
        
    $result=mysql_query($query);
         if (
    mysql_num_rows($result)>0){
             while (
    $row=mysql_fetch_assoc($result)){
                 
    $tempparentstring=$parentstring;
                 
    $tempparentstring=$tempparentstring." >> ".$row['Name'];
                 echo 
    "<option value=\"".$row['ID']."\">";
                 echo 
    $tempparentstring;
                 echo 
    "</option>\n";
                 
    categories($row['ID'],$tempparentstring);
             }
         }
    }
    //------------------
    require ("connect.php"); 
    if (!
    mysql_select_db($databaseName$connection))
            
    showerror();
    echo 
    "<select name=\"Categories\">\n";
    $query="SELECT * FROM categories where parent IS NULL";
    $result=mysql_query($query);
    while (
    $row=mysql_fetch_assoc($result)){
        echo 
    "<option value=\"".$row['ID']."\">";
        echo 
    $row['Name'];
        echo 
    "</option>\n";
        
    categories($row['ID'],$row['Name']);
    }
    echo 
    "</select>\n";
    As for populating the second <select> box, i have no clue...
    anyone have a good idea or solution? thanks!

IMN logo majestic logo threadwatch logo seochat tools logo