I have a javascript alert which is displaying on form submit if a condition occurs. This is fine and I can use the following:
alert("Name You Must Agree To The Terms and Conditions / You have chosen an invalid saving amount");
This is called in the </head> section. I would however like to include a value in the error which is called from a database, which is called further in my asp using:
strErrorSavingsAmount = rs("ErrorSavingsAmount")
Is there a way to display this and/or will it work if the javascript function is in the head and the connection to the database is further down in the code?
To help, here is some more information, this is my full JavaAScript:
<script type="text/javascript">
function validate(str, chk, min, max) {
n = parseFloat(str);
return (chk && !isNaN(n) && n >= min && n <= max);
}
function validateForm()
{
if(!validate(document.forms["myForm"]["Amount"].value,
document.forms["myForm"]["agreesubmit"].checked, 5, 250)) {
alert("Name You Must Agree To The Terms and Conditions / You have chosen an invalid saving amount");
return false;
}
}
</script>
Where would I declare teh variable?
What you are taking about is passing server side variables to client side code. It doesn’t matter where the connection to the database occurs because it will always occur before the JavaScript is executed.
I don’t have much knowledge with ASP but I understand that the convention of code insertion works similarly to PHP. With the
<% %>syntax.All you would have to do is create a JavaScript variable containing the value of a server side variable. I’ll give an example in PHP and hopefully you will see what I mean –
Note how I am wrapping the server variable with additional quotes so that it is compatible with the JavaScript.
Now after you have done this your JavaScript will have access to the
jsVariablethat was created on the server.