dynamically populate select option list via javascript array with sql data

  • Thread starter skywalker skywalker
  • Start date
S

skywalker skywalker

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
 
D

Denis McMahon

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
 
E

Evertjan.

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.CreateParameter("@UserID",
adSmallInt, adParamInput, 2, UserID));
thisCommand.Parameters.Append(thisCommand.CreateParameter("@PgID",
adInteger, adParamInput, 4, PgID));
thisCommand.Parameters.Append(thisCommand.CreateParameter("@Rating",
adTinyInt, adParamInput, 1, Rating)); var rs = thisCommand.Execute();
thisCommand = null;
thisConnection = null;
%>

<http://stackoverflow.com/questions/6377249/using-variables-in-classic-as
p-parameterized-sql>
 
D

Denis McMahon

Denis McMahon wrote on 21 jul 2011 in comp.lang.javascript:

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
 
E

Evertjan.

Denis McMahon wrote on 22 jul 2011 in comp.lang.javascript:
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".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top