Solved

Forcing form refresh - classic ASP

Posted on 2016-09-19
7
93 Views
Last Modified: 2016-09-20
I have a table that displays records from a recordset on one form - `Form1`, and on the form below that - `Form2` - has a couple of textboxes and a button to add records to the recordset. if I print out recordset right after adding a new record, it shows that the record has been added correctly, yet the first form does not get refreshed to reflect a new change. How can I force that refresh usinfg classic ASP?


    <div is="content">
    <%
       Dim RS 
       set rs=Server.CreateObject("ADODB.recordset")
       'code to fill the recordset here
       '.....
    %>
       <form method="POST" name="Form1" id="Form1" action="Page1.asp">
          <table id="dataTbl">
             <tr>
                 <th>Column 1</th>
                 <th>Column 2</th>
             </tr>

	     <%If RS.EOF Then
			Response.Write("No records found")
		Else
			RS.MoveFirst
			While Not RS.EOF
			%>
			<tr>							
				<td><%=RS.Fields("Field1").value%></td>
				<td><%=RS.Fields("Field2").value%></td>		
			</tr>
			<%							
				RS.MoveNext()
			     Wend
		 	End If
			%>
	   </table>
	   <br/>
	   <input type="button" class="button" id="btnSubmit" name="btnSubmit" value="Submit" onClick="btnSubmit_Click()" />
        </form>
        <br/><br/>
            <form method="POST" action="Page1.asp" id="Form2" name="Form2">
								<table>
									<tr>
										<td>First Field:</td>
										<td><input type="text" id="Value1" name="Value1" /></td>
									</tr>
									<tr>
										<td>Second Field:</td>
										<td><input type="text" id="Value2" name="Value2" /></td>
									</tr>
									<tr>
										<td colspan="2"><input type="button" id="btnAdd" name="btnAdd" value="Add" onClick="btnAdd_Click()"/></td>
									</tr>
								</table>
								<%
								Dim val1, val2
								If Len(Request.Form) > 0 Then
								  val1 = request.Form("Value1")
								  val2 = request.Form("Value2")
								 
								  'add a record to recordset
								  With RS
									.AddNew
									.Fields("Field1")=val1
									.Fields("Field2")="val2
									
									.Update				
								End With
								Response.Write RS.RecordCount & "<br/>"
								
								RS.MoveFirst
								While Not RS.EOF
									Response.Write RS.Fields("Field1").value & ":" & RS.Fields("Field2").value & "<br/>"
									RS.Movenext()
								Wend
								End If
								%>
							</form>
							<%
					                ''Here update the recordset with whatever values selected in the table on form1
							RS.Close()
							Set RS = Nothing
							%>
					</div>

Open in new window


And here is my javascript:
    <script type="text/jscript">
			
			function btnSubmit_Click(){
				document.Form1.submit();
			}
			
			function btnAdd_Click(){
				document.Form2.submit();
			}
			
			
		</script>

Open in new window

So the above does add a new record to the Recordset object but the table on the Form1 does not reflect the change. How can I ensure it does?

P.S> The recordset at this point is not connected to an actual database, I just fill it wIth dummy records.
0
Comment
Question by:YZlat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 33

Accepted Solution

by:
Big Monty earned 250 total points
ID: 41805362
you are outputting the entire recordset BEFORE you add the form data to it, so it'll never be fully updated.

moving some code around, i think this'll work:

    <div is="content">
    <%
       Dim RS 
       set rs=Server.CreateObject("ADODB.recordset")
       'code to fill the recordset here
       '.....
    %>
       <form method="POST" name="Form1" id="Form1" action="Page1.asp">
          <table id="dataTbl">
             <tr>
                 <th>Column 1</th>
                 <th>Column 2</th>
             </tr>

	     <%If RS.EOF Then
			Response.Write("No records found")
		Else
			RS.MoveFirst
			While Not RS.EOF
			%>
			<tr>							
				<td><%=RS.Fields("Field1").value%></td>
				<td><%=RS.Fields("Field2").value%></td>		
			</tr>
			<%							
				RS.MoveNext()
			     Wend
		 	End If
			
			Dim val1, val2
			If Len(Request.Form) > 0 Then
			  val1 = request.Form("Value1")
			  val2 = request.Form("Value2")
			 
			  'add a record to recordset
			  With RS
				.AddNew
				.Fields("Field1")=val1
				.Fields("Field2")="val2
				
				.Update				
			End With
		'	Response.Write RS.RecordCount & "<br/>"
			
		'	RS.MoveFirst
			'While Not RS.EOF
			'	Response.Write RS.Fields("Field1").value & ":" & RS.Fields("Field2").value & "<br/>"
		'		RS.Movenext()
		'	Wend
			End If

			%>
	   </table>
	   <br/>
	   <input type="button" class="button" id="btnSubmit" name="btnSubmit" value="Submit" onClick="btnSubmit_Click()" />
        </form>
        <br/><br/>
            <form method="POST" action="Page1.asp" id="Form2" name="Form2">
								<table>
									<tr>
										<td>First Field:</td>
										<td><input type="text" id="Value1" name="Value1" /></td>
									</tr>
									<tr>
										<td>Second Field:</td>
										<td><input type="text" id="Value2" name="Value2" /></td>
									</tr>
									<tr>
										<td colspan="2"><input type="button" id="btnAdd" name="btnAdd" value="Add" onClick="btnAdd_Click()"/></td>
									</tr>
								</table>
							</form>
							<%
					                ''Here update the recordset with whatever values selected in the table on form1
							RS.Close()
							Set RS = Nothing
							%>
					</div>

Open in new window


** i commented out the code to that writes out the newly added data
0
 
LVL 35

Author Comment

by:YZlat
ID: 41805413
Thanks for you fast response!

I have tried moving the code like you have suggested but gotten the same result.

Not sure if I made it clear what I am doing. I have a table that displays data from the recordset that is already there. Then there is a second form that allows the user to add more fields to the recordset. It might or might not be used
0
 
LVL 53

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 200 total points
ID: 41805583
Below is real rough, but essentially  you want to put your logic at the top of the page where first you checkif the form is submitted. If it is, then add a record.  If not, don't add a record.

Now run your recordset and display at the top in your columns and at the bottom in your form fields.

Again, code below is not production, just illustration.

<%
If Len(Request.Form) > 0 Then
    ' add record to db
end if

' create recordset
field1 = rs("field1")
field2 = rs("field2")
%>

<html>
<table>
     <tr>
             <td>col1</td><td>col2</td>
      <tr>
     <tr>
             <td><%=field1%></td><td><%=field2%></td>
      <tr>
</table>
<form>
         <input name="field1" value="<%=field1%>">
        <input name="field2" value="<%=field2%>">
</form>
</html>

Open in new window

0
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 41805586
To add, if you are expecting multiple rows of data, run your recordset in both places, but better to use getrows to place your data to an array, then access your array multiple times without hitting the db.
0
 
LVL 57

Assisted Solution

by:Julian Hansen
Julian Hansen earned 50 total points
ID: 41806263
I believe Big monty and Scott have covered this - Big Monty just needed to move the code a little bit higher as Scott did.

I am interested to know why you have onclick handlers for your submit buttons - you are doing nothing in those functions except the default submit so why not just let the forms submit as they normally would?

A submit button is local to the form it is in - when you click it - it submits the parent form - ignoring any other forms on the page.

Also not sure why you need a submit button for the first form - you are not processing any data - you can achieve the same thing with an <a href> styled like a button that just reloads the page. Not saying you can't use a form - just interested to know the thinking behind it.
0
 
LVL 35

Author Comment

by:YZlat
ID: 41807619
Julian Hansen, actually I will be processing data on the first form as well
0
 
LVL 35

Author Closing Comment

by:YZlat
ID: 41807640
Thank you!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

689 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question