S. Stark S. Stark - 4 months ago 35
Javascript Question

force form submit button click with jquery

Trying to trigger the submit functionality from JQuery. what am I doing wrong? In theory, this should work. I've tried about 4 different ways though.

I have tried


$( form:first ).submit();

$( form:first ).trigger("submit");




<html lang="en">
<title>Database Management</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<script src="http://code.jquery.com/jquery-3.1.0.min.js" integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" crossorigin="anonymous"></script>
table td { border: 1px solid black; }
table td:first-child { text-align: left; }
$(document).ready(function() {
$('#erase').click(function() {
if (confirm("Are you sure that you want to ERASE ALL DATA from the database?"))
$('input#submit').trigger("click"); <!-- HERE -->

$('#update').click(function() {
var appID = $('#updateAppID').val();
var field = $('#fieldName').val();
var value = $('#newValue').val();

if (appID == null || appID == "") {
alert("You must enter the ID number of the entry you wish to modify.");
else if (field == null || field == "") {
alert("You must choose which field you wish to modify.");
else if (value == null || value == "") {
alert("You must choose the new value you wish to appear in the database.");
else {
$('input#submit').trigger("click"); <!-- HERE -->

$('#delete').click(function() {
var appID = $('#deleteAppID').val();

if (appID == null || appID == "") {
alert("You must enter the ID number of the entry you wish to delete.");
else {
$('input#submit').trigger("click"); <!-- HERE -->
<div id="container">
<from id="databaseActionForm" name="databaseActionForm" method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
<table border=0 style="margin: 50px auto; text-align: right;">
<tr style="text-align: center; font-weight: bold; font-size: 1.5em; text-decoration: underline;">
<th>Additional Info</th>
<tr name="Clear">
<td>Erase Database</td>
<td style="text-align: center;">ARE YOU SURE?</td>
<td><input type="button" id="erase" name="erase" value="ERASE ALL?" /></td>
<tr name="Update">
<td>Update Value</td>
Entry ID: <input type="text" id="updateAppID" name="updateAppID" placeholder="App entryID" /><br />
Field Name: <input type="text" id="fieldName" name="fieldName" placeholder="Field to change" /><br />
New Value: <input type="text" id="newValue" name="newValue" placeholder="New value" /><br />
<td><input type="button" id="update" name="update" value="Update Value" /></td>
<tr name="Delete">
<td>Delete Payment</td>
Entry ID: <input type="text" id="deleteAppID" name="deleteAppID" placeholder="App entryID" />
<td><input type="button" id="delete" name="delete" value="Delete Entry" /></td>
<input type="hidden" id="buttonTypePressed" name="buttonTypePressed" />
<input type="submit" id="submit" name="submit" value="submit" style="position: absolute; left: -9999px; width: 1px; height: 1px;" tabindex="-1"/>


There are 2 issues here, 1 is a typo in the element name form, you have it as from.

Another is the name/id of the submit button, it should not be submit as it will override the default submit property(the function) of the form element

<input type="submit" id="bsubmit" name="bsubmit" value="submit" style="position: absolute; left: -9999px; width: 1px; height: 1px;" tabindex="-1" />

Then just use the below snippet to submit the form


Demo: Fiddle