Sunday, March 9

How to Submit Form without Using Submit button in JavaScript

Generally, a form is submitted when the user presses a submit button. However, sometimes, we may need to submit the form without submit button using JavaScript.
JavaScript provides the form object that contains the submit() method. Use the ‘id’ of the form to get the form object.
e.g. if name of the form is ‘f1’, the JavaScript code for the submit call is 

document.forms["myform"].submit();

Here is the code to submit a form when a hyperlink is clicked:

<form name="f1" action="abc.php">
Search: <input type='text' name='t1' />
<a href="javascript: submitform()">Search</a>
</form>
<script type="text/javascript">
function submitform()
{
  document.f1.submit();
  alert("Value is sumitted");
}
</script>

After submitting the values, value which is entered will be shown in abc.php page.

<html>
<body>
<?php
$a=$_POST['t1'];
echo "Submitted values is ".$a;
?>
</body>
</html>