PDA

View Full Version : Executable Code In an Form's OnClick


topher793
08-06-2008, 05:12 PM
I am relatvily new to to asp/vbscript development, i am trying to record the value of a select form an put in into an array value, i have tried by including code like the following onChange="<%change = 1 %> or with the onSubmit, and with the OnClick variety. and later i simply have an if statement that says change = 1. but what happens is the code within the if statement is executed automatically, so it is executing my code onChange="<%change = 1 %> without the click/submit/change trigger. i am not sure what else to try. Thanks for the help

I have pasted my code bellow:


<%
response.Write(cat) %></b></font></td> </tr> <%
newcat = 0
end if
%>
<tr align="center">
<td width="25%" ><a class="thumbnail" href="#thumb"><%=rs("formnum")%><span><img src=<%response.Write(thepic)%> border="0" /></span></a></td>
<td width="25%"><%=rs("Description")%></td>
<td width="25%"><%=rs("Packs Of")%></td>
<td width="25%" valign="middle"><form name="qty<%response.Write(count)%>" action="" >
<select name="qty<%response.Write(count)%>" onChange="<%change = 1 %>">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</form></td>
</tr>
<%

rs.movenext
end if

count = count + 1 ' gives all qty boxes unique names
loop

else
response.Write("<tr align=""center""><td colspan=""8"">There are currently no forms available to order.</td></tr>")
end if





<%

if change = 1 then
response.Write("WORKED")
response.Redirect("google.com")

do while count =>0
response.Write(request.Form("qty"&count))
response.Write("TEST")
count = count -1
loop

end if

%>

Spudhead
08-07-2008, 02:31 PM
You seem to be misunderstanding how ASP works.

ASP is a server-side technology. It outputs HTML (and javascript, and whatever else you tell it to) to the browser.

Event handlers like onChange are client-side. ASP doesn't know anything about them.

Go to your page in your browser, and view source on it. See the onChange bit in your select box? There's nothing in it. By the time the page gets sent to the browser, there's no ASP code in it.

What's happening, as you point out, is that the server is going through your page, finding the ASP bits, and executing those. This is where your variable "change" gets set to 1. Then the server finishes doing any ASP stuff and sends a bunch of HTML back down to the browser.

This is a fundamental aspect of server-side scripting, and it's absolutely critical that you understand fully how, and where, ASP code is executed in relation to the server and to the client browser. If I were you I'd go and spend a little time reading up on ASP on sites like w3schools.com - it might be a pain, but it'll give you the theoretical grounding in how the technology works.

Bullschmidt
09-03-2008, 07:52 AM
Many Web pages "post back" to themselves and then handle further processing from there. This can often cut down on the number of pages that need to be developed and the related back-and-forth involved with many pages.

To make a form post back to the same page:
<form id="frmMain" name="frmMain" action="<%= Request.ServerVariables("SCRIPT_NAME") %>" method="post">

And here are some buttons:
<% ' *** btnClose %>
<input type="submit" name="btnClose" value="Close">
<% ' *** btnSave %>
<input type="submit" name="btnSave" value="Save">
<% ' *** btnNew %>
<input type="submit" name="btnNew" value="New">
<% ' *** btnDel %>
<input type="submit" name="btnDel" value="Delete" onclick="return confirm('Are you sure you want to delete this record?');">

To handle processing toward the top of the page:
If Request.Form("btnClose") <> "" Then ' Close btn.
' Close.
Call ClosePg()
Elseif Request.Form("btnSave") <> "" Then ' Save btn.
' Set var.
Call SetVar("frompost")

' Save rec.
Call SaveRec()
Elseif Request.Form("btnNew") <> "" Then ' New btn from this pg.
' Set var.
Call SetVar("new")
Elseif Request.Form("btnDel") <> "" Then ' Del btn.
' Set var.
Call SetVar("frompost")

' Del rec.
Call DelRec()

' Set var.
Call SetVar("new")
Elseif Request.QueryString("CustID") <> "" Then ' CustID from Many pg.
' Set var.
Call SetVar("fromdb")
Elseif Request.QueryString("CustID") = "" Then ' Add btn from MainMenu pg.
' Set var.
Call SetVar("new")
End If

And the ClosePg(), DelRec(), SaveRec(), and SetVar() functions above are custom functions that would still need to be developed.