Page 1 of 1
php variables with Javascript
Posted: Sun Dec 26, 2004 1:35 pm
by Hibba
Is there some kind of way I can have:
onSubmit="return function('check every variable in the form')"
And then in the head have something like:
function function(contents) {
if (((contents / contents) != 1) && (contents != 0)) {
alert('Please enter only a number into this box \n (No commas or decimals)');
return false;
} else {
return true;
}
}
Thanks for the help!
Posted: Sun Dec 26, 2004 2:50 pm
by nigma
Make the function that validates form fields. Then set the value of the onSubmit attribute to the name of the validating function.
So you'd setup the function:
Code: Select all
<script language="javascript">
function validate_elements() {
// code to validate elements
}
</script>
And then setup the onSubmit attribute of the form:
Posted: Sun Dec 26, 2004 3:36 pm
by Hibba
OK, so right now I have this:
Code: Select all
<head><SCRIPT LANGUAGE="JAVASCRIPT"><!--
function check(thing) {
if (((document.thing.value / document.thing.value) != 1) && (document.thing.value != 0)) {
alert('Please enter only a number into this box \n (No commas or decimals)');
return false;
} else {
return true;
}
}
//--></SCRIPT></head>
<form name ="myform" method="post" action ="question2.html" onSubmit="return check(how_many);">
<p>How many years do you plan to be in your home?</p>
<p><input type="text" name="how_many" size="10"></p>
<input type="hidden" name="passer">
<input type="submit" value="Next" name="submit">
</form>
But it does not work. I think because of how I have the variables, but not really sure how to do that and not sure where to go to have some good help on it.