How to submit form with JavaScript

How to submit form with JavaScript
Published Sep 05,2022 by Kailash Singh
0 Comment 1165 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 )
Elevenstech Web Tutorials
Elevenstech Web Tutorials helps you learn coding skills and enhance your skills you want.
As part of Elevenstech's Blog, Elevenstech Web Tutorials contributes to our mission of “helping people learn coding online”.
Read More
Newsletter
Subscribe to get the latest updates from Elevenstech Web Tutorials and stay up to date