Click to See Complete Forum and Search --> : Preserve Form Contents During Browser Refresh
Illufox
05-16-2005, 04:38 PM
I've built a form with dynamic menus where users can add more items to the menus via a pop up that refreshes the form to show the new menu items. This all works great despite the fact that the users' unfinished input is getting lost (form fields are being reset). :eek:
I did some research on the web and found an answer, unfortunately the solution is about automatic refresh and PHP - I'm using a manual refresh and ASP.
This is the code I found and that I need to adjust:
HEAD section:
<script type="text/javascript">
<!--
var seconds = 5;
onload = function(){
setInterval('refreshWithVars()', seconds*1000);
}
function refreshWithVars() {
var thefield = document.theform.thefield.value;
window.location = 'http://www.yourwebserver.com/index.php?thefield='+thefield;
}
//--></script>
BODY section:
<form name="theform" action="index.php">
<?php
if (isset($_GET['thefield'])) {
$thefield = $_GET['thefield'];
echo "<input name=\"thefield\" value=\"$thefield\">";
}
else {
echo "<input name=\"thefield\">";
}
?>
<input type="submit">
</form>My programming skills are not good enough to adjust this script for my form....
xxx
looks like the javascript is pulling the values from the form fields and posting it to the query string.
change the bottom code to something like this:
<input name="text1" value="<%=response.write("text1")%>">
for each box. if there is no query string value for that box, it just wont put anything in.
Illufox
05-16-2005, 07:52 PM
It I do this, the form field says "text1" before I even put anything in. Instead it should be more something like this:
If page has been refreshed execute this: <%response.write("whatever has been entered")%>
Else do nothing
It seems to easy but I just can't figure it out....
im sorry, response.querystring instead of response.write.
Illufox
05-17-2005, 11:12 AM
The 'Response' object doesn't support this property or method 'QueryString', so I changed it to 'Request.QueryString'. However, when I refresh, the field value is being reset again.
Here is what I have so far (just for one field so far):
HEAD section:
function refreshWithVars() {
var EventName = document.form1.EventName.value;
window.location = 'http://mywebsite.com/spaces/events/insert_test.asp?EventName='+EventName;
}
BODY section:
<body onload="window.name='mainWindow';return true;refreshWithVars()">
.....
....
<input type="text" name="EventName" value="<%=Request.QueryString("EventName")%>" size="32">
I've also tried this where the form field goes:
<%
If Request.QueryString("EventName") Then %>
<input name="EventName" value="<%Request.QueryString("EventName")%>">
<% Else %>
<input name="EventName" value="">
<% End If %>
man, not doing so good today. :( response should be request like you did.
asp doesnt use the curly braces {}
<%
if Request.QueryString("EventName") then
MM_EventName = Request.QueryString("EventName")
Response.Write "<input name=""EventName"" value=""" & MM_EventName & """>"
else
Response.Write "<input name=""EventName"">"
end if
%>
dont think that will make any difference though. on the page after the refresh, do you have anything in the query string?
Illufox
05-17-2005, 11:55 AM
I realized that the code wasn't correct and fixed it shortly after like so:
<% If Request.QueryString("EventName") Then %>
<input name="EventName" value="<%Request.QueryString("EventName")%>">
<% Else %>
<input name="EventName" value="">
<% End If %>
Unfortunately this doesn't make a difference. When I enter a value in the "EventName" field and then refresh the page, the value still disappears.
In PHP they use the $_GET method to pull the value back into the field. I wonder if the PHP code I posted above (it works!) can be translated into ASP??
is there anything in the query string when you refresh the page?
Illufox
05-17-2005, 01:54 PM
Nope, the form field gets reset to empty....
the query string. the url in the browser above. you should see your page and then ?something=somevalue&another=someothervalue etc.
Illufox
05-17-2005, 02:04 PM
Nope, no changes in the URL after I refresh...
thats why you arent getting any values in the forms. the javascript isnt getting values to the query string.
Illufox
05-17-2005, 04:22 PM
I know that the javascript is not correct but I thought that it's not needed if we use the correct ASP in the form. Apparently it's needed so I have to fix it. Unfortunately I didn't get any responses from the JavaScript forum so I'm pretty much on my own here.....:(