Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Autosubmitting a form in VBScript 1

Status
Not open for further replies.

cdck

Programmer
Nov 25, 2003
281
US
I have an ASP page that inserts a new row into the database, queries the identification field on that row, and then redirects to another page with the identification number.

I do this by outputting the number into the URL as a querystring, but I want to avoid using querystrings as much as possible. I'd like to put the number into a hidden field on a form and then immediately submit the form to get to the next page with the number in the form results.

Since I have little experience with Javascript and the rest of this page is VBScript, I'd like to do this in VBScript. The form code would look like:

<FORM method="post" action="PRStartStep1.asp">
<INPUT Type="hidden" value="<% response.write(PRid) %>" name="PR">
</FORM>

How can I make it submit without user interaction?

Cheryl dc Kern
 
You can't. You will need to use JavaScript. And on the onload event you will need to call a javascript function that will submit the page. You'll have to use the window.form.submit() object.
 
How can I pass a variable between VBScript and JavaScript?

Cheryl dc Kern
 
Well technically you CAN write client-side code in VBScript but it isnt a good idea. One reason is that only the Internet Explorer browser will be able to execute it, another is that many people find it less confusing to edit and debug when the server-side and client-side logic are not the same language.
 
I'm confused; I'm not writing ANY client-side code. Or are you saying that all Javascript is client-side? would this mean that if a browser has Javascript turned off, the form auto-submit wouldn't work anymore?

Cheryl dc Kern
 
To pass the variable between VBScript and JavaScript try this:[tt]
<%
Dim VBSomeNum
VBSomeNum = 5
%>
<script>
function JSAddSome(Num)
{
return Num + <% Response.Write VBSomeNum %>;
}
</script>
[/tt]
 
Thank you! I think I have my solution now.

Cheryl dc Kern
 
You can also write server-side code in JavaScript but submitting a form is done by the browser so any code that submits a form is client-side.

It is easy to get confused about which code is running where. That is why many people prefer to write VBScript on the server and JavaScript on the browser.... just to keep it straight. You don't HAVE to do it this way but in my opinion it will save you a lot of work, even if you don't really know JavaScript.
 
Another way to pass the value from your server-side script to the client-side is to write the value into a hidden form element and then read the value into your client-side script. Here is a simple example:
[tt]
<%
Dim VBSomeNum
VBSomeNum = 5
%>
<script>
function DoIt()
{
var TheValue = document.form.foo.value;
alert('The value is: ' + TheValue);
return;
}
</script>
<form name="form" action="page.asp" method="post">
<input type="button" onClick="DoIt();>
<input type="hidden" name="foo" value="<%Response.Write VBSomeNum %>"
</form>
[/tt]
 
That's great, that's exactly what I was looking for, thanks!

Cheryl dc Kern
 
Actually, a better way to handle this entirely would be to not pass the value client-side.

You have a page that is handling the insert and retrieving back a an id. You then need to load a second page with that id. The easiest method would be to use Response.Redirect to redirect to the second page after you store the value in either Response.Session variable or Response.Cookie.

-T

 
Tarwn:

Thanks so much. While I do think that it's just plain time I learn Javascript, this gives me an alternative that makes sense and lets me keep this page in one language - which will help avoid confusion later. I'll have to consider both options as I go forward.

Cheryl dc Kern
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top