dynamically populate select option list via javascript array with sql data

Discussion in 'Javascript' started by skywalker skywalker, Jul 21, 2011.

  1. 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
    skywalker skywalker, Jul 21, 2011
    #1
    1. Advertising

  2. Re: dynamically populate select option list via javascript arraywith sql data

    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
    Denis McMahon, Jul 21, 2011
    #2
    1. Advertising

  3. skywalker skywalker

    Evertjan. Guest

    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>

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Jul 21, 2011
    #3
  4. Re: dynamically populate select option list via javascript arraywith sql data

    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
    Denis McMahon, Jul 22, 2011
    #4
  5. skywalker skywalker

    Evertjan. Guest

    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)
    Evertjan., Jul 22, 2011
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UmV6YSBTb2xvdWtp?=

    Not able to populate and select an item in a list dynamically? Hel

    =?Utf-8?B?UmV6YSBTb2xvdWtp?=, Aug 20, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    358
    Scott Allen
    Aug 25, 2004
  2. julian
    Replies:
    3
    Views:
    193
    Lord Unspecified
    Nov 5, 2003
  3. Mark Kolber
    Replies:
    4
    Views:
    330
  4. jzhang29
    Replies:
    1
    Views:
    143
  5. Robert Oschler
    Replies:
    1
    Views:
    120
    Robert Oschler
    Sep 3, 2005
Loading...

Share This Page