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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onClick event

Status
Not open for further replies.

mmarkym

Programmer
Mar 23, 2002
54
US
Why doesn't the sub work when called from the <input type='button' name='myButton' value='click me'>

<%


'This won't work, I get nothing
sub myButton_OnClick
response.write &quot;hello world&quot;

end sub


'From here it works

dim strFname
dim strLname
dim strStreet
dim strCity
dim strState
dim intZip
dim intNum
dim intTime
dim intDate
dim strEmail
dim strConnectionString
dim rsObj
dim connObj
dim strQuery



strFname = request.form(&quot;fname&quot;)
strLname = request.form(&quot;lname&quot;)
strStreet = request.form(&quot;street&quot;)
strCity = request.form(&quot;city&quot;)
strState = request.form(&quot;state&quot;)
intZip = request.form(&quot;zip&quot;)
intNum = request.form(&quot;number&quot;)
intTime = request.form(&quot;time&quot;)
intDate = request.form(&quot;date&quot;)
strEmail = request.form(&quot;email&quot;)

response.write &quot;<P>&quot;
response.write &quot;<B>&quot; & &quot;<center>&quot; & &quot;<font color='navy' size='5pt'>&quot;

response.write &quot;<table border='1' bordercolor='#800000' width='30%' bgcolor='cccccc'>&quot;
response.write &quot;<th colspan='2' align='center'>&quot; & &quot;Reservations for &quot; & strLname & &quot;!&quot; & &quot;<P>&quot;

response.write &quot;<TR>&quot; & &quot;<TD align='center'>&quot;
response.write &quot;<B>First Name:</B> &quot; & &quot;</TD><TD align='center'>&quot; & strFname & &quot;</TD></tr>&quot;
response.write &quot;<TR>&quot; & &quot;<TD align='center'>&quot;
response.write &quot;<B>Last Name</B> &quot; & &quot;</TD><TD align='center'>&quot; & strLname & &quot;</TD></TR>&quot;
response.write &quot;<TR>&quot; & &quot;<TD align='center'>&quot;
response.write &quot;<B>Street:</B> &quot; & &quot;</TD><TD align='center'>&quot; & strStreet & &quot;</TD></tr>&quot;
response.write &quot;<TR>&quot; & &quot;<TD align='center'>&quot;
response.write &quot;<B>City/Town:</B> &quot; & &quot;</TD><TD align='center'>&quot; & strCity & &quot;</TD></tr>&quot;
response.write &quot;<TR>&quot; & &quot;<TD align='center'>&quot;
response.write &quot;<B>State:</B> &quot; & &quot;</TD><TD align='center'>&quot; & strState & &quot;</TD></tr>&quot;
response.write &quot;<TR>&quot; & &quot;<TD align='center'>&quot;
response.write &quot;<B>Zip Code:</B> &quot; & &quot;</TD><TD align='center'>&quot; & intZip & &quot;</TD></tr>&quot;
response.write &quot;<TR>&quot; & &quot;<TD align='center'>&quot;
response.write &quot;<B>Number of party:</B> &quot; & &quot;</TD><TD align='center'>&quot; & intNum & &quot;</TD></tr>&quot;
response.write &quot;<TR>&quot; & &quot;<TD align='center'>&quot;
response.write &quot;<B>Time:</B> &quot; & &quot;</TD><TD align='center'>&quot; & intTime & &quot;</TD></tr>&quot;
response.write &quot;<TR>&quot; & &quot;<TD align='center'>&quot;
response.write &quot;<B>Date:</B> &quot; & &quot;</TD><TD align='center'>&quot; & intDate & &quot;</TD></tr>&quot;
response.write &quot;<TR>&quot; & &quot;<TD align='center'>&quot;
response.write &quot;<B>Email:</B> &quot; & &quot;</TD><TD align='center'>&quot; & strEmail & &quot;</TD></tr>&quot;

response.write &quot;<tr><td align='center'>&quot; & &quot;<input type='button' name='myButton' value='Submit''>&quot; & &quot;</td><td align='center'>&quot; & &quot;<input type='reset' value='Start over'>&quot; & &quot;</td></tr>&quot;

response.write &quot;</table>&quot;
response.write &quot;</font>&quot; & &quot;</center>&quot; & &quot;</B>&quot;



%>
 
ASP Pages are processed on the server, and the resulting output sent to the browser, at which time processing stops.

To make this work, you would need browser-side scripting. This could be done in Javascript like so:

Code:
<input type='button' name='myButton' value='click me' OnClick='doMsg()'>

<SCRIPT Language=&quot;JavaScript&quot;>
function doMsg {
   Alert(&quot;Hello, World!&quot;);
}
</script>
... in this way, processing is done at the client end, instead of the server.

You *could* do it in ASP, however you would have to have the button call another page....

Code:
<input type='button' name='myButton' value='click me' action='someotherpage.asp'>

... someotherpage.asp
<%
Response.Write &quot;Hello, world&quot;
%>
... in this way, clicking on the button calls another page, the browser loads the new page.

A cool thing to do is to have multiple submit buttons on a page, each doing a different thing (I use that method for doing a spell checker button on forms) If you'd like to see how that's done, let me know.

--Greg

 
Sure - I'd like to see how that's done. It can always come in useful in the future.

mark
 
It's actually pretty easy. (Maybe this should be done as a tip instead???)

Set up your form as you always would in ASP:
Code:
<form name=&quot;myform&quot; action=&quot;somepage.asp&quot; method=&quot;post&quot;>
... some form junk in here...

<input type=&quot;submit&quot; name=&quot;s1&quot; value=&quot;Do spell check&quot; onClick=&quot;dospellcheck()&quot;>

<input type=&quot;submit&quot; name=&quot;s2&quot; value=&quot;Submit Form&quot; onClick=&quot;submitit()&quot;>

... Now, the secret is a little bit of JavaScript on the page, that actually CHANGES the action of the form when it's submitted... something like this (in the <head> area):

Code:
<script language=&quot;JavaScript&quot;>
function dospellcheck()
{
    document.myform.action=&quot;someotherasp.asp&quot;;
    document.myform.submit();
}
function submitit()
{
    document.myform.action=&quot;somepage.asp&quot;;
    document.myform.submit();
}
</script>

... and that's all there is to it! When the submit button is pressed, it replaces the action portion of the form with the one defined in the script, and then submitting the form.

In my application, I use it for spell-check, so included in my script is a command to open a sub-window (so my form still stays on the screen). The code I'm using is like this:

Code:
function SubmitSpellCheck1()
{
	winStats='toolbar=no,location=no,directories=no,menubar=no,'
	winStats+='width=400,height=250,left=200,top=100,scrollbars=yes'
	spellchk=window.open(&quot;&quot;,&quot;spellchk&quot;,winStats)
	document.MAIN.action=&quot;spellchk.asp?section=DATA&quot;
	document.MAIN.target=&quot;spellchk&quot;
	document.MAIN.submit();
}

Hope this stuff helps!
--Greg

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top