0

Hi,

I'm new to this and have a quick question.

I have a form called register.html and have javascript to check the form validation

<form
<input type="submit" value="Submit" onClick="return formValidate()">
</form>

so in register.html when user presses submit the formValidate method (javascript) is run.

once it returns true, how do i pass the variables from the form into a php page?

i know i can change onClick="data.php" but then the JS is not being run ...

Thanks

3
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by Alxandr
0

Have onsubmit event for form. Here is ane example.

<?php 
if(isset($_REQUEST['submit'])) {
	print "<pre>";
	print_r($_REQUEST);
	print "</pre>";
}
?>
<html>
<head>
<script type='text/javascript'>
function validate(form) {
	if(form.name.value == "") {
		alert("Name cannot be empty");
		return false;
	}
	return true;
}
</script>
</head>
<body>
<form method='post' onSubmit='javascript: return validate(this);'>
Name: <input type='text' name='name'><br />
Age: <input type='text' name='age'><br />
<input type='submit' name='submit' value='submit'>
</form>
</body>
</html>

The part in <?php if($_REQ...) can be in your data.php .

Good luck!

0

thanks.

i've decided to place all the code in a php file instead of using a php and html file.

makes it easier! :)

1

Hi,

I'm new to this and have a quick question.

I have a form called register.html and have javascript to check the form validation

<form
<input type="submit" value="Submit" onClick="return formValidate()">
</form>

so in register.html when user presses submit the formValidate method (javascript) is run.

once it returns true, how do i pass the variables from the form into a php page?

i know i can change onClick="data.php" but then the JS is not being run ...

Thanks

Not to disagree that it's simpler having all in one file and that, but I just thought I might explain a little so that you understand how it works for further reference. The onclick of the submit only determines weather or not the submit is sent at all, it has nothing to do with where the form is posted. To create a form you enter this code:

<form action="page_that_should_accept_form.php_or_something" method="post_or_get">
</form>

When that form is submitted it's sent to the page that's addressed inside the action-attribute using the method specified within the method-attribute.

Then let's add the submit-button:
(this is inside the form)

<input type="submit" value="Send form" onclick="return validateform();" />

That is one way you could add validation. What happens when a user presses the submit-button, the onclick-attribute's content is fired as javascript. If the resulting evaluated javascript returns false or 0 (I think that's all), nothing will happen. If it return anything else the submit-method of the form will be called, first invoking onsubmit if any is found, then performing the submit.

As nav33n wrote, another way to get the same effect is to add the method to the onsubmit-method of the form itself. This is probably the preferred method to handle the situation, because then you can have several submit-buttons etc., but if you have the method in both onclick and onsubmit it will be fired twice (which may not be a big problem, but it will take longer time).

Hope that helps.

Votes + Comments
Good explanation. :)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.