The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> JavaScript Development
|
need reference on creating javascript arrays from query
Discuss need reference on creating javascript arrays from query in the JavaScript Development forum on Dev Shed. need reference on creating javascript arrays from query JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 29th, 2002, 08:31 AM
|
|
ÐÊšîGñË®
|
|
Join Date: Apr 2001
Posts: 66
Time spent in forums: < 1 sec
Reputation Power: 13
|
|
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.
|

August 29th, 2002, 08:50 AM
|
 |
Contributing User
|
|
Join Date: Jun 2002
Location: Washington, DC
|
|
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
|

August 29th, 2002, 09:03 AM
|
|
ÐÊšîGñË®
|
|
Join Date: Apr 2001
Posts: 66
Time spent in forums: < 1 sec
Reputation Power: 13
|
|
|
Javascript does not do database queries last time i checked. The javascript will be written from a PHP/MySQL query.
|

August 29th, 2002, 09:18 AM
|
 |
Contributing User
|
|
Join Date: Jun 2002
Location: Washington, DC
|
|
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......
|

August 29th, 2002, 09:56 AM
|
|
ÐÊšîGñË®
|
|
Join Date: Apr 2001
Posts: 66
Time spent in forums: < 1 sec
Reputation Power: 13
|
|
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.
|

August 29th, 2002, 12:08 PM
|
 |
Feelin' Groovy
|
|
Join Date: Aug 2001
Location: WDSMIA
|
|
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.
|

August 29th, 2002, 04:02 PM
|
|
ÐÊšîGñË®
|
|
Join Date: Apr 2001
Posts: 66
Time spent in forums: < 1 sec
Reputation Power: 13
|
|
|
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.
|

August 29th, 2002, 04:10 PM
|
|
ÐÊšîGñË®
|
|
Join Date: Apr 2001
Posts: 66
Time spent in forums: < 1 sec
Reputation Power: 13
|
|
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>
|

August 29th, 2002, 04:51 PM
|
 |
Feelin' Groovy
|
|
Join Date: Aug 2001
Location: WDSMIA
|
|
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.
|

August 30th, 2002, 04:18 PM
|
|
ÐÊšîGñË®
|
|
Join Date: Apr 2001
Posts: 66
Time spent in forums: < 1 sec
Reputation Power: 13
|
|
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!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|