Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > dynamically populate select option list via javascript array with sql data

Reply
Thread Tools

dynamically populate select option list via javascript array with sql data

 
 
skywalker skywalker
Guest
Posts: n/a
 
      07-21-2011
Dear Javascript guru,

I need to create a dynamic javascript array based on the select from
sql data

Firstly, I have 3 tables and its data fields:

tblTable1:
T1-ID, T1-Name


tblTable2:
T2-ID, T2-Name, T1-ID


tblTable3:
T3-ID, T3-Name, T2-ID


Secondly, I have html code with 3 select box:

T1: <SELECT Name="T1" onChange="updateT2()">
<option></option>

here I will have while loop to loop T1 table to load the T1-ID
and T1-Name
<option value="<%=RS("T1-ID")%>"><%=RS("T1-Name")</option>

</SELECT>


T2: <SELECT Name="T2" onChange="updateT3()">
<option></option>
</SELECT>

T3: <SELECT Name="T3">
<option></option>
</SELECT>


T2 and T3 options list will depend on the selection of T1

Once T1 is selected, the T1-ID will checked against T2 table, to
populate T2-ID and T2-Name into T2 Select option list.

Once T2 is selected, the T2-ID will checked against T3 table, to
populate T3-ID and T3-Name into T3 Select option list.

So, I need to do this in javascript updateT2() and updateT3()



function updateT2()
{
var arrT2 = new array();

...

}

function updateT3()
{
var arrT3 - new array();
..
}



Can you advise?

Regards,
Sky
 
Reply With Quote
 
 
 
 
Denis McMahon
Guest
Posts: n/a
 
      07-21-2011
On Wed, 20 Jul 2011 20:41:06 -0700, skywalker skywalker wrote:

> I need to create a dynamic javascript array based on the select from sql
> data


> Can you advise?


First of all you need to get the data from your sql database into a
format that javascript can use.

However, as this is usually a server side thing, you will probably not
not be using javascript for this, but instead some server side scripting
technology that interfaces to whatever sql based database you are using.

As such, doing so is outside the scope of the comp.lang.javascript
newsgroup.

Having said that, what I usually try and do with php is convert the sql
query output into an indexed array of associative arrays, and then
serialise this as a json string. I then embed the json string within a
call to the parse method of the JSON object to create a new javascipt
object.

I am then able to refer to the sql sourced data using, for example:

jsValue = tableName[index].fieldName;

where jsValue is the variable or property that I want to assign the data
from my sql table to
tableName is the javascript object I created using the parse method of
the JSON object, representing the source table of the original sql query
[index] is a numerical index into the javascript object, representing a
record number in the result of the original sql query
fieldName is a property representing the associative array index into the
records returned in the original sql query

Rgds

Denis McMahon
 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      07-21-2011
Denis McMahon wrote on 21 jul 2011 in comp.lang.javascript:

> However, as this is usually a server side thing, you will probably not
> not be using javascript for this, but instead some server side
> scripting technology that interfaces to whatever sql based database
> you are using.
>
> As such, doing so is outside the scope of the comp.lang.javascript
> newsgroup.


Not so.

Despite PHP-addicts saying this over and over to defend their addiction,
serverside Javascript can very well be used for this.

As such,
this NG is not constrained to clinetside or even in-browser Javascript.

=====================================

Complex example of Classic ASP parameterized SQL in Javascript:

<%
var sqlReview = "DECLARE @UserID AS Int, @PgID AS Int, @Rating AS
TinyInt;"

sqlReview += "SELECT @UserID = ?, @PgID = ?, @Rating = ?;"

sqlReview += "DELETE FROM PGrating WHERE (UserID = @UserID) AND (PgID =
@PgID);"

sqlReview += "INSERT INTO PGrating (InsertDate, PgID, UserID,
Rating) VALUES (GETDATE(), @PgID, @UserID, @Rating);"

var thisConnection = Server.CreateObject("ADODB.Connection");
thisConnection.connectionString = connectString;
thisConnection.Open();

var thisCommand = Server.CreateObject("ADODB.Command");
thisCommand.ActiveConnection = thisConnection;
thisCommand.CommandText = sqlReview;
thisCommand.CommandType = adCmdText;
thisCommand.Parameters.Append(thisCommand.CreatePa rameter("@UserID",
adSmallInt, adParamInput, 2, UserID));
thisCommand.Parameters.Append(thisCommand.CreatePa rameter("@PgID",
adInteger, adParamInput, 4, PgID));
thisCommand.Parameters.Append(thisCommand.CreatePa rameter("@Rating",
adTinyInt, adParamInput, 1, Rating)); var rs = thisCommand.Execute();
thisCommand = null;
thisConnection = null;
%>

<http://stackoverflow.com/questions/6...-in-classic-as
p-parameterized-sql>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Denis McMahon
Guest
Posts: n/a
 
      07-22-2011
On Thu, 21 Jul 2011 22:36:13 +0000, Evertjan. wrote:

> Denis McMahon wrote on 21 jul 2011 in comp.lang.javascript:
>
>> However, as this is usually a server side thing, you will probably not
>> not be using javascript for this


>> As such, doing so is outside the scope of the comp.lang.javascript
>> newsgroup.


> Not so.


Well, if as I surmised initially he's not using js for the server side
database access, then how he gets the data from the sql database is
indeed outside the scope of this newsgroup.

Do many people actually use server side js?

Rgds

Denis McMahon
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      07-22-2011
Denis McMahon wrote on 22 jul 2011 in comp.lang.javascript:

> On Thu, 21 Jul 2011 22:36:13 +0000, Evertjan. wrote:
>
>> Denis McMahon wrote on 21 jul 2011 in comp.lang.javascript:
>>
>>> However, as this is usually a server side thing, you will probably
>>> not not be using javascript for this

>
>>> As such, doing so is outside the scope of the comp.lang.javascript
>>> newsgroup.

>
>> Not so.

>
> Well, if as I surmised initially he's not using js for the server side
> database access, then how he gets the data from the sql database is
> indeed outside the scope of this newsgroup.


Indeed PHP is off topic, if yopu ment that with "doing so".

> Do many people actually use server side js?


No, only few people program.
Even less program serverside.
Of those some, oh abomination, use VBS or PHP.

However people interested in this NG and who program serverside
and who love and are proficient in programming in js,
they know how easy it is to [test and] use the same functions both
serverside and clientside.

=========================

Or do you mean "people use" as in
"people using websites that are serverside programmed in js"?

=========================

I often use a combination of serverside js and serverside vbs,
calling often used js-functions as black boxes from a vbs main
structure. Why do I do that? Primarily because it is just fun to do so.
But then, I don't believe in "best way programming".

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
OPTION elements added to a dynamically created SELECT box via DOM are not visible in IE Robert Oschler Javascript 1 09-03-2005 10:28 AM
How can I dynamically populate data when selecting an option from a dropdown list? jzhang29 Javascript 1 02-24-2005 06:15 AM
Dynamically update SELECTED option in option list Mark Kolber Javascript 4 01-01-2005 02:07 PM
Not able to populate and select an item in a list dynamically? Hel =?Utf-8?B?UmV6YSBTb2xvdWtp?= ASP .Net 1 08-25-2004 05:40 PM
dynamically populate drop down menu option value problem julian ASP General 3 11-05-2003 09:50 PM



Advertisments