Click to See Complete Forum and Search --> : how to call a ASP function in the onclick event


andrewchai
03-29-2005, 10:36 PM
good morning !!!
i already try this but can not call the function.

<html>
<head><title>Leave Taken Form</title>
<%
function calculate()

if request.QueryString("fdate") <> "" then
response.Redirect("login_invalid.asp")
end if

end function
%>"

</head>
<body>


<form method="get" onSubmit="<% if request.QueryString("fdate") <> "" then response.Redirect("add_to_ltran.asp") end if %>">


<input type="submit" name="Submit" value="Submit Form" />
<input type="button" name="button22" value="calculate" onclick=" calculate()" />

</form>
</body>
</html>

HaganeNoKokoro
03-29-2005, 11:06 PM
ASP functions execute only on the server side, while the onclick event executes only on the client side. You are trying to mix the two in an impossible way.

The problem is that the function calculate() does not exist on the client-side. The browser cannot see it.

I'd like to help you, but I don't know what this is supposed to do. All I know is right now, if the page is loaded and the fDate QueryString variable is not an empty string, it will redirect to a page called "add_to_ltran.asp". But placing this code inside the onsubmit handler of the form is meaningless. From your browser, view the source code (usually you can do this from the view menu or by right-clicking on the page). You will see what the browser sees, and maybe it will help you understand why your code does not work.

If you can explain what the page is supposed to do, I may be able to help you.

phpnovice
03-29-2005, 11:10 PM
No, you cannot call an ASP function directly from an HTML event. If you want to go to that trouble, then you have to use remote calls via an object like this -- for IE:

var req = new ActiveXObject("Microsoft.XMLHTTP");

and an object like this for Mozilla-based browsers

var req = new XMLHttpRequest();

andrewchai
03-29-2005, 11:35 PM
thanks for your reply !

can i write the ASP code in the form onclick event. for IE:

<input type="button" name="test" value="calculate"
onclick = " <% if request.QueryString("fdate") <> "" then
response.Redirect("login_invalid.asp")
end if %> " />

buntine
03-29-2005, 11:39 PM
Nope. You are misunderstanding the concept of server-side programming. The statement you are printing will be executed before the page is even output to the clients machine.

You will need to use JavaScript (or another client-side technology) if you want to have this type of functionality.

Regards.

phpnovice
03-29-2005, 11:57 PM
can i write the ASP code in the form onclick event.
No, you cannot put ASP code in an onclick event and expect that code to execute only when the button is clicked. Instead, that code will always execute before the page is sent to the browser and, therefore, before you even have a chance to click such a button.

andrewchai
03-30-2005, 01:50 AM
Thanks!

how to refresh the form using the javascript function.

phpnovice
03-30-2005, 09:14 AM
self.location.reload(true);

mattmortimer
10-10-2006, 10:55 AM
I know this post is old but this may help someone.

Put your button inside a <FORM> tag and post it to another page. In the other page put something like this:

<%
If Request.Form > 0 Then
WriteFav()
Response.Redirect("http://anypageyouwant.com")
End If
%>


I've used this and it works. In fact the page your form posts to can be completly empty since it is redirecting to another page after it calls the ASP function. Assuming your ASP funtion precesses quickly you will hardly notice the redirects.

seobean
10-12-2006, 04:53 AM
mattmoretimer is provided the best answer. It is working fine.

http://www.softbean.com
http://www.offshorebean.com
http://www.contentbean.com
http://www.logobean.com