Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit Form Without Javascript

I have a form that relies on javascript to do all the client side form validation. The form is submitted via javascript if all the fields are correctly filled in as opposed to using a standard "submit" button:

<button name="button" type=button onclick="validateReqFields('mForm', 'username, password, compPassword, firstName, lastName');">Register</button>

document[formName].submit();

If the client has javascript disabled, all of the form validation is done server side (its actually performed again regardless but that doesn't really matter). The problem lies with using a button with a type of button instead of submit. It works perfect with javascript, but how do I get around this when javascript is not available? If I use a submit button along with the javascript then it submits the form with each button press and doesn't work properly.

like image 258
ryandlf Avatar asked Oct 17 '11 03:10

ryandlf


2 Answers

Use a submit button instead of the "button" button, and then have your validateReqFields function return false if the form is not valid.

Then take out the form submit from the validateReqFields if you like.

This way

  • If the form is valid, then the button click will bubble and the form will submit
  • If the form is invalid, then the javascript will cancel the button click
  • If javascript is disabled, then it will be submitted to the server as a fallback
like image 180
Matt Tew Avatar answered Nov 15 '22 14:11

Matt Tew


Change the type attribute to submit.

<button name="button" type="submit" ... />
like image 45
alex Avatar answered Nov 15 '22 14:11

alex