Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

problem with some code

  • 08-04-2003 12:58PM
    #1
    Closed Accounts Posts: 495 ✭✭


    Hi,

    if anyone could help me out here I would much appreciate it.

    I am trying to create some dependent listboxes on a form, the idea is that you choose an entry from listbox one, and depending on your choice, a limited amount of choices apear in listbox two.

    The listbox options are pulled out of an SQL database, and depending on the amount of entries in the table, the code will write the options into the resulting html page.

    here is the snippet of code:
    function populateState()
    {

    <%
    var rs = Server.CreateObject("ADODB.Recordset");
    rs.Open("select * from State", "DSN=Northwind_SQL");
    var n=0;
    while (!rs.EOF)
    {

    var stateID = rs.Fields("State_ID");
    var stateDesc = rs.Fields("State_DESC");

    Response.Write ("document.frmTest.cboState[" + n + "] = new Option('" + stateDesc+ "’,’" + stateID + "’);" );


    if (new String(strState).search(stateID) != -1)
    Response.Write("document.frmTest.cboState[" + n + "].selected = true;");
    rs.MoveNext();
    n++;
    }
    rs.Close();
    %>

    }

    The result of the Response.Write in the resulting html is as follows:

    function populateState()
    {

    document.frmTest.cboState[0] = new Option('Clare ’,’1’);document.frmTest.cboState[1] = new Option('Limeric ’,’2’);document.frmTest.cboState[2] = new Option('Galway ’,’3’);document.frmTest.cboState[3] = new Option('Cork ’,’4’);

    }

    as you can see, all options are on one single line, this is preventing the listboxes to be populated, has anyone any ideas as to what is wrong?

    Regards.


Comments

  • Registered Users, Registered Users 2 Posts: 912 ✭✭✭chakotha


    I am more a home with PHP than ASP/VBScript but you could try replacing the line
    Response.Write ("document.frmTest.cboState[" + n + "] = new Option('" + stateDesc+ "’,’" + stateID + "’);" );

    with
    Response.Write ("document.frmTest.cboState[" + n + "] = new Option('" + stateDesc+ "’,’" + stateID + "’);\n" );

    The 'backslash n' should put each option on a separate line but dunno if this will make the Javascript fill the options

    Hope that helps!


  • Moderators, Politics Moderators, Paid Member Posts: 44,051 Mod ✭✭✭✭Seth Brundle


    add &vbcrlf to the end of where you want a new line
    [personally speaking you don't need the semicolon at the end of each line with ASP and I believe '&' works better than '+' when joining two strings.]
    i.e.

    function populateState()
    {

    <%
    var rs = Server.CreateObject("ADODB.Recordset");
    rs.Open("select * from State", "DSN=Northwind_SQL");
    var n=0;
    while (!rs.EOF)
    {

    var stateID = rs.Fields("State_ID");
    var stateDesc = rs.Fields("State_DESC");

    Response.Write ("document.frmTest.cboState[" + n + "] = new Option('" + stateDesc+ "’,’" + stateID + "’);" & vbcrlf );


    if (new String(strState).search(stateID) != -1)
    Response.Write("document.frmTest.cboState[" + n + "].selected = true;");
    rs.MoveNext();
    n++;
    }
    rs.Close();
    %>

    }


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    Originally posted by kbannon
    Response.Write ("document.frmTest.cboState[" + n + "] = new Option('" + stateDesc+ "’,’" + stateID + "’);" & vbcrlf );
    I've not looked at this code in detail, but I can say that given he's using JScript and not VBScript, he'd want to use "\n" rather tha vbCrLf.


  • Moderators, Politics Moderators, Paid Member Posts: 44,051 Mod ✭✭✭✭Seth Brundle


    actually I reckon you are right - too quick for my own good!


Advertisement