How to submit form with JavaScript

Kailash Singh

How to submit form with JavaScript

Published Sep 05,2022 by Kailash Singh

0 Comment     688 Views    


In this tutorial, we are going to teach you, How to submit form with JavaScript.

Create a <form id='myForm'> and <select> element with languages <option>. Define submitForm() function on change event of <select>. When it gets triggered then submit the form by calling submit() method and set an option selected using PHP according to the selected value.

 

Example:

<form method='post' action='' id='myForm'>
 Select Language : <select name='lang' onchange='submitForm();'> 
  <option value='hindi' <?php if(isset($_POST['lang']) && $_POST['lang'] == 'hindi'){ echo "selected"; } ?> >Hindi</option> 
  <option value='english' <?php if(isset($_POST['lang']) && $_POST['lang'] == 'english'){ echo "selected"; } ?> >English</option>
  <option value='punjabi' <?php if(isset($_POST['lang']) && $_POST['lang'] == 'punjabi'){ echo "selected"; } ?> >Punjabi</option> 
 </select> 
</form>
 
<!-- Script --> 
<script type='text/javascript'> 
function submitForm(){ 
  // Call submit() method on <form id='myform'>
  document.getElementById('myform').submit(); 
} 
</script>

 


Comments ( 0 )