JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 29th, 2002, 08:31 AM
true2u1 true2u1 is offline
ÐÊšîGñË®
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2001
Posts: 66 true2u1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 13
Send a message via ICQ to true2u1 Send a message via AIM to true2u1
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.
__________________
richard.php?code=php&db=mysql&ws=apache&skill=5

Last edited by true2u1 : August 29th, 2002 at 09:58 AM.

Reply With Quote
  #2  
Old August 29th, 2002, 08:50 AM
jpenn's Avatar
jpenn jpenn is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jun 2002
Location: Washington, DC
Posts: 2,692 jpenn User rank is Sergeant (500 - 2000 Reputation Level)jpenn User rank is Sergeant (500 - 2000 Reputation Level)jpenn User rank is Sergeant (500 - 2000 Reputation Level)jpenn User rank is Sergeant (500 - 2000 Reputation Level)jpenn User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 5 h 41 m 10 sec
Reputation Power: 20
Quote:
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

Reply With Quote
  #3  
Old August 29th, 2002, 09:03 AM
true2u1 true2u1 is offline
ÐÊšîGñË®
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2001
Posts: 66 true2u1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 13
Send a message via ICQ to true2u1 Send a message via AIM to true2u1
Javascript does not do database queries last time i checked. The javascript will be written from a PHP/MySQL query.

Reply With Quote
  #4  
Old August 29th, 2002, 09:18 AM
jpenn's Avatar
jpenn jpenn is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jun 2002
Location: Washington, DC
Posts: 2,692 jpenn User rank is Sergeant (500 - 2000 Reputation Level)jpenn User rank is Sergeant (500 - 2000 Reputation Level)jpenn User rank is Sergeant (500 - 2000 Reputation Level)jpenn User rank is Sergeant (500 - 2000 Reputation Level)jpenn User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 5 h 41 m 10 sec
Reputation Power: 20
Quote:
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......

Reply With Quote
  #5  
Old August 29th, 2002, 09:56 AM
true2u1 true2u1 is offline
ÐÊšîGñË®
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2001
Posts: 66 true2u1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 13
Send a message via ICQ to true2u1 Send a message via AIM to true2u1
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/...rays/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.

Reply With Quote
  #6  
Old August 29th, 2002, 12:08 PM
Yawmark's Avatar
Yawmark Yawmark is offline
Feelin' Groovy
Dev Shed God 11th Plane (10000 - 10499 posts)
 
Join Date: Aug 2001
Location: WDSMIA
Posts: 10,135 Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 4 Days 1 h 22 m 45 sec
Reputation Power: 5052
Send a message via ICQ to Yawmark Send a message via MSN to Yawmark
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: http://forums.devshed.com/showthrea...&threadid=42512
__________________
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)));}}

Last edited by Yawmark : August 29th, 2002 at 12:22 PM.

Reply With Quote
  #7  
Old August 29th, 2002, 04:02 PM
true2u1 true2u1 is offline
ÐÊšîGñË®
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2001
Posts: 66 true2u1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 13
Send a message via ICQ to true2u1 Send a message via AIM to true2u1
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.

Reply With Quote
  #8  
Old August 29th, 2002, 04:10 PM
true2u1 true2u1 is offline
ÐÊšîGñË®
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2001
Posts: 66 true2u1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 13
Send a message via ICQ to true2u1 Send a message via AIM to true2u1
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> 

Reply With Quote
  #9  
Old August 29th, 2002, 04:51 PM
Yawmark's Avatar
Yawmark Yawmark is offline
Feelin' Groovy
Dev Shed God 11th Plane (10000 - 10499 posts)
 
Join Date: Aug 2001
Location: WDSMIA
Posts: 10,135 Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level)Yawmark User rank is General 61st Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 4 Days 1 h 22 m 45 sec
Reputation Power: 5052
Send a message via ICQ to Yawmark Send a message via MSN to Yawmark
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.

Reply With Quote
  #10  
Old August 30th, 2002, 04:18 PM
true2u1 true2u1 is offline
ÐÊšîGñË®
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2001
Posts: 66 true2u1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 13
Send a message via ICQ to true2u1 Send a message via AIM to true2u1
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > need reference on creating javascript arrays from query

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap