Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Does MoveNext work in a loop in Javascript?

    Hope I can get some help here...

    I connect to an SQL database using ASP. I can see the records. I have a small (very basic) function written in Javascript. When I use the MoveNext in the function, it works fine. However, when I use it inside of a loop, the MoveNext does not work. Is this a known problem? Please help.

    This is where I connect to the database from ASP:

    <%
    Dim DeptID
    Dim DeptID_numRows

    Set DeptID = Server.CreateObject("ADODB.Recordset")
    DeptID.ActiveConnection = MM_A_CRS_STRING
    DeptID.Source = "SELECT * FROM dbo.mstrPersonnel ORDER BY ID ASC"
    DeptID.CursorType = 0
    DeptID.CursorLocation = 2
    DeptID.LockType = 2
    DeptID.Open()

    DeptID_numRows = 0
    %>

    Now here is the function is Javascript:

    The MoveNext command NEVER works inside of the LOOP but works anywhere else in the function.

    <script type="text/javascript">
    function Owner() {
    <% DeptID.MoveFirst() %>;
    var y = "<%= Session("TotalRec") %>"; // is total number of record in recordset
    alert ("y from session = " + y);
    <% DeptID.MoveFirst() %>;
    alert(" <%=(DeptID.Fields.Item("DeptID").Value)%>" + " from pull dowm = " + document.frmCRSContract.Department_ID.value + " Current Record ID = " + "<%=(DeptID.Fields.Item("ID").Value)%>")
    <%DeptID.MoveNext()%>;
    alert(" <%=(DeptID.Fields.Item("DeptID").Value)%>" + " from pull dowm = " + document.frmCRSContract.Department_ID.value + " Current Record ID = " + "<%=(DeptID.Fields.Item("ID").Value)%>")

    var x = 1;
    while (x <= y)
    {
    <%DeptID.MoveNext()%>; // this command does NOT work in the loop but works outside of it!
    alert(" in loop Current deptID = " + "<%=(DeptID.Fields.Item"DeptID").Value)%>" + " from pull dowm = " + document.frmCRSContract.Department_ID.value + " Current Record ID = " + "<%=(DeptID.Fields.Item("ID").Value)%>");
    x = x + 1
    } // End of LOOP
    alert(" deptID = " + "<%=(DeptID.Fields.Item("DeptID").Value)%>");
    } // end Function

    </script>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    <%DeptID.MoveNext()%>; // this command does NOT work in the loop but works outside of it!
    alert(" in loop Current deptID = " + "<%=(DeptID.Fields.Item("DeptID").value)%>" + " from pull dowm = " + document.frmCRSContract.Department_ID.value + " Current Record ID = " + "<%=(DeptID.Fields.Item("ID").Value)%>");
    Last edited by Philip M; 04-26-2007 at 04:09 PM.

  • #3
    New Coder
    Join Date
    Dec 2006
    Posts
    80
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I'm not that familiar with ASP but I do work with javascript and ADO SQL. That said, how many records are being returned to the recordset object? If you call MoveNext when the current record is the last record, it will error out.

    Joe
    http://www.Kanotix.com --My OS of choice, the best hardware detection on the planet!-
    http://distrowatch.com -Linux is the future-
    http://www.packetstormsecurity.org -Security Gurus-

  • #4
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you sooooo much for the reply and taking the time to help. It is not the first record. It stays at the first record until the loop ends. This is the reason I included the ALERT statment to convince myself that the records are being read properly.

    The MOVENEXT just does not work inside of the loop and I cannot figure out why.

    Thanks again!!!

  • #5
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I forgot the mention the number of records. There are over 3000! This statment alert ("y from session = " + y); displays the total number of records in the recordset.

    The 1st MoveNext commands work when they are outside of the loop. But not inside!

    Thanks

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Have you checked the syntax error (missing bracket) I pointed out to you?

  • #7
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you Sir. It was not the missing bracket in which case the script would have produce a syntax error at run time. I omitted the braket when I was copying and pasting from the source code in Dreamweaver. Thanks again.

  • #8
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    At the time asp code is executed, javascript code does not yet exist. The javascript is running on the client, and the asp is running on the server, you can't interact with the 2 scripts in the way that you are. You need to do an asp loop and loop through the recordset. To be frank this is all buffuddled (no offense meant). What exactly are you trying to accomplish.
    Helping to build a bigger box. - Adam Matthews

  • #9
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Dear Moderator:

    Thanks for the feedback. I am well aware that ASP runs on the server while Javascript runs on the client. If however the problem was client-server related, I would NOT have been able to issue the MoveNext command while outside the loop. The function is called when the content of some fields have are changed on a form (think of OnChange).

    <select name="Department_ID" class="NewData" id="Department_ID" onChange="Owner()">


    I am sorry if this sounds a little confusing. Here is what I was trying to accomplish. It is very basic and it fact, I have implemented the same in MS-Access and PL/1 on the mainframe. It is just that, the environment I am supporting now requires all forms to be created and maintained using Dreamweaver. My management all wants all codes to be Javascript. Please do not ask me why.

    Now, in the database I have the following fields for example:

    ID, NAME, ADDRESS, SSN, PHONE etc...

    From a pulldown withing a form, the user, select and ID and all other fields NAME, ADDRESS etc... must populate the corresponding fields in the same form.

    To that effect, I create a javascript function called Owner. It loops through the entire record set until it finds an ID in the database that matches the one selected in the pulldown. I have removed some of the codes for the sake of simplicity. If for example, I issue the command <%DeptID.MoveNext()%> 100 times manually and I check for the DeptID each time, I will find a match. If however, I loop 100 times issuing <%DeptID.MoveNext()%>, (using Do While etc...) the cursor NEVER moves to the next record. It says at record 1.

    This the problem I am having. Thanks again for your help!

  • #10
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts

    Arrow

    The problem is client server related. It is not possible to do what you are trying to do without:

    A. Refresh the page passing the id of the record you want, then re-querying the database to retrieve the desired data and then display it as the page renders. (most logical)

    or

    B. Make an xmlhttp request to another page that will return the data, which can then be parsed and displayed through javascript. (I like this, but it's my thing, if it's not your thing there is a bit of a learning curve)

    or

    C. Retrieve all the data for all records initially, house them in a JS array perhaps and then use JS to display the appropriate record when the dropdown changes. (not a good choice)

    Here's what I'm trying to convey about the current state of your script:

    Look at your JS function, after the page loads, the MoveNext inside the loop, does not exist, MoveNext doesn't write anything to the page, so it has absolutley no effect on the javascript. If you view source after the page loads this is what you will see (where it says "deptid" would be your actual id):
    Code:
    <script type="text/javascript"> 
    function Owner() { 
    var y = "total number of records"; // is total number of record in recordset 
    alert ("y from session = " + y); 
    alert("deptid" + " from pull dowm = " + document.frmCRSContract.Department_ID.value + " Current Record ID = " + "deptid") 
    alert("deptid" + " from pull dowm = " + document.frmCRSContract.Department_ID.value + " Current Record ID = " + "deptid") 
    
    var x = 1; 
    while (x <= y) 
    { 
    // this command does NOT work in the loop but works outside of it!
    alert(" in loop Current deptID = " + "deptid" + " from pull dowm = " + document.frmCRSContract.Department_ID.value + " Current Record ID = " + "deptid"); 
    x = x + 1 
    } // End of LOOP 
    alert(" deptID = " + "deptid"); 
    } // end Function 
    
    </script>
    So you see, when you call the js function you have no communication with the server, the recordset does not exist. All the movenext in the loop does at this time, is move the recordset up one upon the load of the page, once. It doesn't care that it is placed inside a javascript loop, javascript however does care if it is placed inside an asp loop. I hope that makes sense.

    If you need an example I can provide one. Just let me know.
    Last edited by Basscyst; 04-26-2007 at 08:57 PM.
    Helping to build a bigger box. - Adam Matthews

  • #11
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks again.

    But here is my question. If the MoveNext does not work in Javascript, why does it work outside the loop?

    Again, it works in the function but NOT inside the ""while (x <= y)" loop. This is the main reason we are all puzzled here. Believe me, the command works outside the loop (as absurd as it may sound).

    Now, in the possible solutions that you proposed, how would I implement A and B? How can I load another page from Javascript when the content of a field has changed?

    Thankl

  • #12
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    The movenext should be working now, inside the loop, it is just only moving it next, once.

    Give me a sec I'll muster up an example. It will be non working but it will hopefully shed some light.
    Helping to build a bigger box. - Adam Matthews

  • #13
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks. I am waiting for the example...

    You if the function was like this:

    <script type="text/javascript">
    function Owner() {
    <% DeptID.MoveFirst() %>;
    var y = "<%= Session("TotalRec") %>"; // is total number of record in recordset
    alert ("y from session = " + y);
    <% DeptID.MoveFirst() %>;
    alert(" <%=(DeptID.Fields.Item("DeptID").Value)%>" + " from pull dowm = " + document.frmCRSContract.Department_ID.value + " Current Record ID = " + "<%=(DeptID.Fields.Item("ID").Value)%>")

    <%DeptID.MoveNext()%>;
    alert(" <%=(DeptID.Fields.Item("DeptID").Value)%>" + " from pull dowm = " + document.frmCRSContract.Department_ID.value + " Current Record ID = " + "<%=(DeptID.Fields.Item("ID").Value)%>")

    <%DeptID.MoveNext()%>;
    alert(" <%=(DeptID.Fields.Item("DeptID").Value)%>" + " from pull dowm = " + document.frmCRSContract.Department_ID.value + " Current Record ID = " + "<%=(DeptID.Fields.Item("ID").Value)%>")

    <%DeptID.MoveNext()%>;
    alert(" <%=(DeptID.Fields.Item("DeptID").Value)%>" + " from pull dowm = " + document.frmCRSContract.Department_ID.value + " Current Record ID = " + "<%=(DeptID.Fields.Item("ID").Value)%>")

    <%DeptID.MoveNext()%>;
    alert(" <%=(DeptID.Fields.Item("DeptID").Value)%>" + " from pull dowm = " + document.frmCRSContract.Department_ID.value + " Current Record ID = " + "<%=(DeptID.Fields.Item("ID").Value)%>")

    <%DeptID.MoveNext()%>;
    alert(" <%=(DeptID.Fields.Item("DeptID").Value)%>" + " from pull dowm = " + document.frmCRSContract.Department_ID.value + " Current Record ID = " + "<%=(DeptID.Fields.Item("ID").Value)%>")

    <%DeptID.MoveNext()%>;
    alert(" <%=(DeptID.Fields.Item("DeptID").Value)%>" + " from pull dowm = " + document.frmCRSContract.Department_ID.value + " Current Record ID = " + "<%=(DeptID.Fields.Item("ID").Value)%>")
    }

    I can have 1000 of the above statments and they work fine from the JavaScript function.
    The MoveNext command works each time. It will go to the next record and prints the corresponding record in the database. It is ONLY WHEN I PUT IT IN A LOOP THAT IT DOES NOT WORK!!!

  • #14
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    Edit:
    Yep, that works, because as the page is loading and rendering the javascript to the page, you are moving through the recordset and writing the value of the current record to the alert. As you make each new alert, you are moving again to the next record.


    This is in vbscript, but the concept is the same. There is one form that posts to the existing page, and some code at the top that only runs when the first form has been submitted.

    Then it takes the id that the first form submits, makes a new query specific to the id and pulls the required data, and sets it to variables. Then those variables are placed as values in the second form.

    Code:
    <html>
    	<head>
    		<%
    			recordid=Replace(Request("recordid"),"'","''")
    			If recordid <> "" Then
    				set rs2=("select * from yourtable WHERE recordid='" & recordid & "')
    				If not rs.EOF Then
    					username=rs2.Fields("username").value
    					address=rs2.Fields("address").value
    					phone=rs2.Fields("phone").value
    					'Now you can do what you wish with these variables as the page loads
    				End If
    			End If
    			
    			set rs=("Select recordid from yourtable");
    		%>
    	</head>
    	
    	<body>
    		<form method="post">
    			<select name="recordid">
    			<%
    				Do Until rs.EOF
    			%>
    				<option value="<%=rs.Fields("recordid").value%>"><%=rs.Fields("recordid").value%></option>
    			<%
    				rs.MoveNext
    				Loop
    				rs.close
    				set rs=nothing
    			%>	
    			</select>
    			<input type="submit" value="Go" />
    		</form>
    		<form method="post" action="the_page_that_saves_your_data.asp">
    			User:<input type="text" name="username" value="<%=address%>" />
    			Address: <input type="text" name="username" value="<%=username%>" />
    			Phone: <input type="text" name="username" value="<%=phone%>" />
    			<br />
    			<input type="submit" value="Save" />
    		</form>
    	</body>
    </html>
    Make sense? I'm off to lunch, back in a few.

    Basscyst
    Last edited by Basscyst; 04-26-2007 at 09:25 PM.
    Helping to build a bigger box. - Adam Matthews

  • #15
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Are you familiar with NetMeeting? Do you think we could set up a Netmeeting session? My IP address is 169.188.236.77.

    Please advise...


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •