My goal is to make a post CRUD that has a variable amount of input fields. With a normal CRUD setup I run in to the problem that the variable fields vanish when the user refreshes the page or when the post doesn't get through the validation. To fix this I was thinking about using ajax to make it possible to save the added input fields directly into the database. How would I go about this?
The input fields belong to the post so the post should exist when saving the input fields. Should I start with an empty saved post to accomplish this or is this bad practice?
Posts in the db:
post
- id
- title
Input fields in the database:
input field
- id
- post_id
- title
- body
How I see it:
- user creates a new post (this stores an empty post in the database).
- the input fields are added and stored in the database using ajax with its own CRUD on the create page
- user fills in any forms and input fields also with ajax so it can be updated in between.
- when the post is stored any empty input fields are destroyed.