The onclick attribute of submit input and the onsubmit attribute of form

A form typically has an input button of the type sumbit; clicking the submit input button may trigger the posting of the form. The submit input button can have an onclick attribute that runs some js code on click; the form can have an onsubmit attribute that runs some js code on form submission. Both attributes aim to do some checking/validation before the data on the form are sent to the server.

<form action="test.php" method="post" onsubmit="return mysumbit();">
    <input type="submit" onclick="return myfun(); ">
</form>

The content of onclick attribute and onsubmit attribute is a piece of javascript code. It can be a single statement or several statements. It can return a value (one of the statement is the return statement) or doesn’t return any value(no return statement).

In case the js code returns a false value, the post-process won’t be triggered. So, if myfun() returns a false value, the code in the onsumbit attribute of the form will not be executed and the form will not be submitted. If myfun() returns a true value, the code in the onsumbit attribute of the form will be executed and if that code also returns true, the form will be submitted to the server. Of course, if the code in the onsumbit attribute of the form returns false, the form won’t be submitted, either.

In case the js code in the onclick attribute or the onsubmit attribute returns true, the post-process will be triggered. For the onclick attribute of the submit input, the post-process is to call the code of the onsubmit attribute of the form. For the onsubmit attribute of form, the post-process will be submitting the form data to the server.

If the js code in the onclick attribute/onsubmit attribute doesn’t return anything(no return statement), the post-process is also triggered like it returns a true value.

Did you like this?
Tip admin with Cryptocurrency

Donate Bitcoin to admin

Scan to Donate Bitcoin to admin
Scan the QR code or copy the address below into your wallet to send some bitcoin:

Donate Bitcoin Cash to admin

Scan to Donate Bitcoin Cash to admin
Scan the QR code or copy the address below into your wallet to send bitcoin:

Donate Ethereum to admin

Scan to Donate Ethereum to admin
Scan the QR code or copy the address below into your wallet to send some Ether:

Donate Litecoin to admin

Scan to Donate Litecoin to admin
Scan the QR code or copy the address below into your wallet to send some Litecoin:

Donate Monero to admin

Scan to Donate Monero to admin
Scan the QR code or copy the address below into your wallet to send some Monero:

Donate ZCash to admin

Scan to Donate ZCash to admin
Scan the QR code or copy the address below into your wallet to send some ZCash:

Leave a Reply