How to submit form with JavaScript Share
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>





Click to Join









Comments 0