Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form submit from JavaScript is not working

I have a form on a page I want to submit using JavaScript:

<script>
  function deleteFromlist(listname, listvalue)
  {
    if (confirm("Are you sure you want to delete this?")) {
       document.getElementById('fromList').value=listname;
       document.getElementById('deleteRecord').value=listvalue;
       document.getElementById('watchlistForm').submit(); // Fails here
    }
  }
</script>

<form method='POST'
      id='watchlistForm'
      enctype='multipart/form-data'
      action='process.php'>

  <input type='text' name='fromList' id='fromList' value=''>
  <input type='text' name='deleteRecord' id='deleteRecord' value=''>
  <input type='submit' name='submit' value='submit'>
</form>

The JavaScript function is called from a href in an table elsewhere on the page. It correctly populates the input fields, but it fails with this message:

TypeError: document.getElementById(...).submit is not a function
document.getElementById('watchlistForm').submit();

If I replace the doc....submit(); with

alert(document.getElementById('watchlistForm').innerHTML

then I get the expected output in the alert box. Hence

document.getElementById('watchlistForm')

does resolve to the form element.

Adding a submit button on the form works as expected.

Why is this not working and how can I fix it?

like image 686
symcbean Avatar asked Aug 25 '16 15:08

symcbean


People also ask

Why is my submit button not working JavaScript?

Sometimes the problem is caused by old versions of the Javascript files, cached by your browser and can be fixed by clearing the browser cache. You can use the browser console of your browser for debugging. After the Javascript error is fixed, the submit button will automatically be enabled.

Why form submit is not working?

The NUMBER ONE error is having ANYTHING with the reserved word submit as ID or NAME in your form. If you plan to call . submit() on the form AND the form has submit as id or name on any form element, then you need to rename that form element, since the form's submit method/handler is shadowed by the name/id attribute.

Can I use JavaScript to submit a form?

The method form. submit() is used for dynamic creation and sending the details to the server. The JavaScript form submission can be used for object creation and various attributes can also be used. The attributes can be class, id, tag, etc.

What is correct way to submit a form using JavaScript?

In javascript onclick event , you can use form. submit() method to submit form. You can perform submit action by, submit button, by clicking on hyperlink, button and image tag etc. You can also perform javascript form submission by form attributes like id, name, class, tag name as well.


1 Answers

Erk, it seems that the button named 'submit' was masking out the method. Changing:

 input type='submit' name='submit' value='submit'

to

 input type='submit' name='submitButton' value='submit'

solved the problem.

like image 128
symcbean Avatar answered Oct 11 '22 22:10

symcbean