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. Advertisements

  2. 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. Advertisements

  3. skywalker skywalker

    Evertjan. Guest

    Denis McMahon wrote on 21 jul 2011 in comp.lang.javascript:
    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., Jul 21, 2011
    #3
  4. 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:
    Indeed PHP is off topic, if yopu ment that with "doing so".
    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., Jul 22, 2011
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.