php variables with Javascript

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Hibba
Forum Commoner
Posts: 58
Joined: Fri Oct 29, 2004 11:37 pm

php variables with Javascript

Post 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!
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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() &#123;
  // code to validate elements
&#125;
</script>
And then setup the onSubmit attribute of the form:
Hibba
Forum Commoner
Posts: 58
Joined: Fri Oct 29, 2004 11:37 pm

Post by Hibba »

OK, so right now I have this:

Code: Select all

<head><SCRIPT LANGUAGE="JAVASCRIPT"><!--
function check(thing) &#123;
   	if (((document.thing.value / document.thing.value) != 1) && (document.thing.value != 0)) &#123;
		alert('Please enter only a number into this box \n (No commas or decimals)');
		return false;
	&#125; else &#123;
	return true;
	&#125;
&#125;
//--></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.
Post Reply