Best Practice for Form Processing

I was wondering what is best for processing forms?

In my case, I am doing something like this:

  • if the user has not submitted the form
    • display form
  • still
    • if there are form errors
      • display errors
      • display form again
    • still
      • display success message
      • (and) display the form again

My problem is that I repeat the HTML code for the form 3 times and I don't think this is good practice (a long file that is hard to read).

+3
source share
7 answers

You must follow the Post / Redirect / Get pattern to avoid duplicate forms.

:

if POST_REQUEST:
  ERRORS = VALIDATE_FORM()
  if ERRORS IS EMPTY:
    PROCESS_REQUEST
    REDIRECT TO <Successful URL>

DISPLAY_FORM(ERRORS)

:

  • URL- :
    POST_REQUEST , ( )
  • :
    POST_REQUEST - , . ERRORS (, ). ERRORS ,
  • :
    POST_REQUEST , . , ERRORS , < URL > .

, .

Django Framework .

VALIDATE_FORM(), , , .

( ) emtpy-.

, , , DISPLAY_FORM():

<ul class="errors"><?php foreach(ERRORS['field_name'] as $error) { echo "<li>$error</li>"; } ?></ul>
<input name="field_name" type="text" ... />
+5

. , HTML- . , , , , . (PHP?) , .

+2

/ .

+1

, , !
:

      • 302 regirect, HTTP

( , ), , get ( ) ()

! .

+1

script :

  • ,

0

, HTML5 .

0

Given that in each case you are displaying a form, can you just display the same form by default, and then add additional features like error reporting, etc.?

What other technologies do you use?

-1
source

Source: https://habr.com/ru/post/1748544/


All Articles