form submission through javascript

It’s good to submit HTML forms through JavaScript, because many offenders try to submit HTML forms by bypassing JavaScript validations through disabling JavaScript. JavaScript’s submit() function used here to submit form to the required page.

Example

<!DOCTYPE HTML>
<html>
<head>
<title>JavaScript form submissions</title>
<script>
	function validateMyForm() 
	{
		var x = document.forms["Form1"]["input1"].value;
		var y = document.forms["Form1"]["input2"].value;
		if (x == "" || y == "") 
		{
			alert("fill out both fields");
			return false;
		} 
		else 
		{
			// submitting form
			document.getElementById('Form1').submit();
		}
	}
</script>
</head>
<body>
	<form name="Form1" id="Form1" action="secondPage.php" method="get">
		<table>
			<tr>
				<td>enter name1</td>
				<td><input type="text" name="input1" id="input1" size="40" /></td>
			</tr>
			<tr>
				<td>enter place</td>
				<td><input type="text" name="input2" id="input2" size="40" /></td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td><input type="button" onClick="return validateMyForm()" value="submit"></td>
			</tr>
		</table>
	</form>
</body>
</html>

here’s page to display result

<!DOCTYPE HTML>
<html>
    <head>
        <title>values are</title>
    </head>
        <body>
            <?php
                echo $_GET['input1'];
                echo $_GET['input2'];
            ?>
        </body>
</html>