Best practice to deal with form processing

2020-06-24 06:51发布

I was wondering what is the best practice to deal with form processing?

In my case I do something like that:

  • if the user hasn't submited the form
    • display the form
  • else
    • if there are form errors
      • display errors
      • display form again
    • else
      • display a success message
      • (and) display the form again

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

标签: php html forms
7条回答
Deceive 欺骗
2楼-- · 2020-06-24 07:05

I would also note that the use of pattern and HTML5 is quite a good practice for avoiding form submission with invalid data.

查看更多
我想做一个坏孩纸
3楼-- · 2020-06-24 07:13

Given that in each case you're displaying the form, can you simply display the same form by default and then add the extras such as error messaging etc?

What other technologies are you using?

查看更多
兄弟一词,经得起流年.
4楼-- · 2020-06-24 07:14

You should follow the Post/Redirect/Get pattern to prevent form submission duplicates.

You can integrate that pattern with error processing and successful message display this way:

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

DISPLAY_FORM(ERRORS)

These are the possible scenarios:

  • Get request at form url:
    POST_REQUEST is false and so you just display the form with no errors (so a normal empty form)
  • Form submission with errors:
    POST_REQUEST is true, you validate the form. In this case some errors will be returned in the ERRORS variable (probably an array). The ERRORS variable is not empty so you just display the form with the error messages displayed
  • Correct form submission:
    POST_REQUEST is true and you validate the form. Since this time the form is valid the ERRORS variable would be empty and you perform the redirect to the <Successful URL> which will display the successful message to the user.

Using this pattern you prevent that the user to send again form data when refreshing the page after a correct form submission.

Django Framework suggest to handle forms this way.

A good way to handle errors in form display is to have the VALIDATE_FORM() function returning an associative array of errors where each key is the name of the field and the corresponding item is an array of error messages.

General form errors (not associated with a specified field) should have an emtpy key.

This way you can, for example, display errors this way in the DISPLAY_FORM() function:

<ul class="errors"><?php foreach(ERRORS['field_name'] as $error) { echo "<li>$error</li>"; } ?></ul>
<input name="field_name" type="text" ... />
查看更多
爷的心禁止访问
5楼-- · 2020-06-24 07:17

You can use the same form code and just display the error / success message above the form.

查看更多
别忘想泡老子
6楼-- · 2020-06-24 07:17

you can break your script in two:

  • one displays the form

  • one gets form data, checks it and returns errors or the success message

查看更多
Summer. ? 凉城
7楼-- · 2020-06-24 07:21

Your workflow looks correct. I don't see why you need to repeat the HTML code for the form. Restructure your code so that the success message is displayed at the top of the form if present and the errors are displayed next to the input boxes if they're present. At the top of your (PHP?) file, process everything and set the variables that will be displayed in the form below.

查看更多
登录 后发表回答