#1
  1. No Profile Picture
    ʚGˮ
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Posts
    66
    Rep Power
    14

    Post need reference on creating javascript arrays from PHP


    I am working on a form that needs to have 1 dynamic dropdown that changes 2 other text fields with a JavaScript onChange. The dropdown and text fields will use 3 JavaScript arrays created by PHP populated from 3 fields in a table. Can anyone point me to any tutorials that might help? I don't need anyone to write any code but helpful suggestions would be greatly appreciated.
    Last edited by true2u1; August 29th, 2002 at 09:58 AM.
    richard.php?code=php&db=mysql&ws=apache&skill=5
  2. #2
  3. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Jun 2002
    Location
    Washington, DC
    Posts
    2,692
    Rep Power
    22
    need reference on creating javascript arrays from query
    Did you post this in the jScript forum? And, if its not PHP please don't post in this forum as you might get flamed......
    ~ Joe Penn
  4. #3
  5. No Profile Picture
    ʚGˮ
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Posts
    66
    Rep Power
    14
    Javascript does not do database queries last time i checked. The javascript will be written from a PHP/MySQL query.
    richard.php?code=php&db=mysql&ws=apache&skill=5
  6. #4
  7. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Jun 2002
    Location
    Washington, DC
    Posts
    2,692
    Rep Power
    22
    I am working on a form that needs to have 1 dynamic dropdown that changes 2 other text fields with a JavaScript onChange.
    ???

    What do you need help with, the SQL query or the jScript?

    The way you posted your subject says you are looking for help with the jScript......
    ~ Joe Penn
  8. #5
  9. No Profile Picture
    ʚGˮ
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Posts
    66
    Rep Power
    14
    I read a decent article on DevShed that explains, but I am looking for other ideas, articles and input. The article is

    http://www.devshed.com/Server_Side/M...ays/page1.html

    And yes I am looking for PHP help!!!!!!! NOT JavaScript or SQL query..... just forget the subject line, ok? All you have to do is read the entire message because the subject is relevant to the message.
    richard.php?code=php&db=mysql&ws=apache&skill=5
  10. #6
  11. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    WDSMIA
    Posts
    10,135
    Rep Power
    5053
    I don't know of any tutorials, but I think the gist of what you're asking for can be accomplished by the following:
    • Query the database for the values of your array
    • Iterate through the result set to populate the array (echo statements work fine)
    In looking further, you could probably just use PHP form the dropdowns and skip the JS arrays, depending on what you're doing (although the same technique can be used for both)...
    PHP Code:
    // $rs is the result of your query
    // assuming MySQL
    echo "<form><select>\n";
    while (
    $row mysql_fetch_row($rs)) {
        echo 
    "<option value=\"" $row[0] . "\">" $row[0] . "</option>\n";
    }
    echo 
    "</select></form>\n"
    // Here's another thread about this very thing: Dynamic form selects
    Last edited by Yawmark; August 29th, 2002 at 12:22 PM.
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  12. #7
  13. No Profile Picture
    ʚGˮ
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Posts
    66
    Rep Power
    14
    Creating dropdowns from a query is no problem for me.

    What I want to do is use an onChange on the dropdown box containing employee names retrieved from a table and call a function that changes the text in 2 text fields for phone number and department, which is also retrieved from the table.

    I'm sure this has to be done with javascript, which means that my PHP coding must create a javascript array for it to work.

    I have been working on the scripting since posting with limited success. I wanted to do this to minimize errors on a form employees submit. Me thinks this project might need to be reconsidered. My javascript is very rusty since I have been heavy into PHP.
    richard.php?code=php&db=mysql&ws=apache&skill=5
  14. #8
  15. No Profile Picture
    ʚGˮ
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Posts
    66
    Rep Power
    14
    Here is my code for creating the javascript array

    PHP Code:
    <?
        mysql_connect
    (    $db_server$db_username$db_password);
        
    mysql_select_db $intranet_db ) or die ("Error opening database");
        
        
    $result mysql_query("SELECT id, last_name, first_name, department, extension FROM telephone ORDER BY last_name");
        
    $numRows mysql_num_rows($result);
        
        PRINT (
    "<script language=\"JavaScript 1.2\">\n");
        PRINT (
    "var numRows = $numRows;\n");
        for(
    $x 0$x $numRows$x++)
            {
            
    $row mysql_fetch_row($result);
            
    $passrow[$x] = $row[0].", ".$row[1].", ".$row[2].", ".$row[3].", ".$row[4];
            }
        
        for(
    $i 0$i $numRows$i++)
            {
            PRINT (
    "emparray[$i] = new Array(\"$passrow[$i]\");\n");
            }
        
        PRINT (
    "</script>");
    ?>
    and here is the dropdown script

    PHP Code:
    <SELECT NAME="empname" SIZE="1" onChange="swaptext(this.form.empname.value)">
        <?
        mysql_connect
    (    $db_server$db_username$db_password);
        
    mysql_select_db $intranet_db ) or die ("Error opening database");
            
        
    $result2 mysql_query("SELECT last_name, first_name, department, extension FROM telephone ORDER BY last_name");
        
    $numRows2 mysql_num_rows($result2);
        for(
    $i 0$i $numRows2$i++)
        {
        
    $row2 mysql_fetch_row($result2);
        PRINT 
    "<option value=\"$i\">".$row2[0].", ".$row2[1]."\n";
        }
        
    ?>
        </SELECT>
    richard.php?code=php&db=mysql&ws=apache&skill=5
  16. #9
  17. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    WDSMIA
    Posts
    10,135
    Rep Power
    5053
    May I suggest the following?
    PHP Code:
    /*-- Your array --*/
    mysql_connect$db_server$db_username$db_password); 
    mysql_select_db $intranet_db ) or die ("Error opening database"); 
    $result mysql_query("SELECT id, last_name, first_name, department, extension FROM telephone ORDER BY last_name"); 
    $numRows mysql_num_rows($result); 
    PRINT (
    "<script language=\"JavaScript 1.2\">\n"); 
    PRINT (
    "var numRows = $numRows;\n"); 
    $count 0;
    while (
    $row mysql_fetch_row($result) { 
        echo 
    "emparray[$count] = new Array(\"" $row[0].", ".$row[1].", ".$row[2].", ".$row[3].", ".$row[4]."\");\n");
        
    $count++; 
    }
    PRINT (
    "</script>"); 

    /*-- Your dropdown --*/
    $result2 mysql_query("SELECT last_name, first_name, department, extension FROM telephone ORDER BY last_name"); 
    while (
    $row2 mysql_fetch_row($result2)) {
        PRINT 
    "<option value=\"$i\">".$row2[0].", ".$row2[1]."\n"

    Not a big deal, but it may speed things up (there's still room for improvement in my examples). And forgive the errors - I don't have a test environment here...
    Last edited by Yawmark; August 29th, 2002 at 04:54 PM.
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  18. #10
  19. No Profile Picture
    ʚGˮ
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Posts
    66
    Rep Power
    14
    Coding is fixed! Here is my code for the curious...

    PHP Code:
    <?
        mysql_connect
    $db_server$db_username$db_password); 
        
    mysql_select_db $intranet_db ) or die ("Error opening database"); 
        
        
    $result mysql_query("SELECT id, last_name, first_name, department, extension FROM telephone ORDER BY last_name"); 
        
    $numRows mysql_num_rows($result); 
        
        PRINT (
    "<script language=\"JavaScript\">\n"); 
        PRINT (
    "\n");
        PRINT (
    "function employee(id, lastName, firstName, department, extension)\n");
        PRINT (
    "{\n");
        PRINT (
    "this.id = id;\n");
        PRINT (
    "this.lastName = lastName;\n");
        PRINT (
    "this.firstName = firstName;\n");
        PRINT (
    "this.department = department;\n");
        PRINT (
    "this.extension = extension;\n");
        PRINT (
    "}\n\n");
        PRINT (
    "function swapText(sel)\n");
        PRINT (
    "{\n");
        PRINT (
    "document.foqform.empphone.value = employeeList[sel].extension;\n");
        PRINT (
    "document.foqform.dept.value = employeeList[sel].department;\n");
        PRINT (
    "}\n\n");
        
        
    $count 0;
        PRINT (
    "var employeeList = new Array();\n");
        while (
    $row mysql_fetch_row($result))
            { 
            PRINT (
    "employeeList[$count] = new employee(\"$row[0]\", \"$row[1]\", \"$row[2]\", \"$row[3]\", \"$row[4]\");\n");
            
    $count++; 
            }
        
        PRINT (
    "</script>"); 
    ?>

    <SELECT NAME="empname" SIZE="1" onChange="swapText(empname.value)">
                                <?
                                mysql_connect
    (    $db_server$db_username$db_password);
                                
    mysql_select_db $intranet_db ) or die ("Error opening database");
                                
                                
    $result2 mysql_query("SELECT last_name, first_name, department, extension FROM telephone ORDER BY last_name");
                                
    $numRows2 mysql_num_rows($result2);
                                
    $count 0;
                                while (
    $row2 mysql_fetch_row($result2))
                                    {
                                    PRINT 
    "<option value=\"$count\">".$row2[0].", ".$row2[1]."\n";
                                    
    $count++;
                                }
                                
    ?>
                                </SELECT>
    Thanks for your assistance, Yawmark!
    richard.php?code=php&db=mysql&ws=apache&skill=5

IMN logo majestic logo threadwatch logo seochat tools logo