Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

problem with some code

Options
  • 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 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 Posts: 39,950 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 Posts: 39,950 Mod ✭✭✭✭Seth Brundle


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


Advertisement