0

While building an API to learn how they work, I've run into an issue when hitting the endpoint which adds data to the underlying database using an HTML form.

Here is the form's HTML:

            <form action="mobile/add" method="post">
                <legend>Add Mobile</legend>
                    <ol>
                        <li>
                        <label for="name">name</label>
                            <input type="text" name="name" id="name"></input>
                        </li>
                        <li>
                            <label for="model">model</label>
                            <input type="text" name="model" id="model"></input>
                        </li>
                        <li>
                            <label for="color">color</label>
                            <input type="text" name="color" id="color"></input>
                        </li>                               
                    </ol>
                <button type="submit">Submit</button>
                <button type="reset">Cancel</button>
            </form>

take note of action="mobile/add"

This form is set up to be visible at this URL:

enter image description here

Here is the form, populated with sample data, prior to clicking the Submit button: enter image description here

When Submit is clicked, the data gets added to the database, as can be seen from the endpoint http://localhost:8080/miscellaneous/APIexamples/CRUD/mobile/list that lists the mobile data:

enter image description here

However, after the form is submitted, the user sees this: enter image description here

I believe the redirect is occurring due to the action="mobile/add" in the form, which is necessary to add the data to the database, but not what I want the user to see.

Is there a way to add the data from the form to the database but redisplay http://localhost:8080/miscellaneous/APIexamples/CRUD/ with the "Add Mobile" form fields cleared out? Or is the approach used above non-standard (ie. maybe this is normally handled using AJAX instead of a form submission)?

Outcome: Thanks to @VoiceOfUnreason's answer, changing the HTTP status code to 205 corrected the issue. The data was still submitted but the page did not redirect. However, the form fields were not cleared out. To do that the form tag was modified as follows <form action="mobile/add" method="post" onsubmit="this.submit();this.reset();return false;">. That bit of wisdom came from @atesin's 8/17/2019 post in this thread.

knot22
  • 2,648
  • 5
  • 31
  • 51

1 Answers1

0

Is there a way to add the data from the form to the database but redisplay http://localhost:8080/miscellaneous/APIexamples/CRUD/ with the "Add Mobile" form fields cleared out?

RFC 7231 describes 205 Reset Content:

The 205 (Reset Content) status code indicates that the server has fulfilled the request and desires that the user agent reset the "document view", which caused the request to be sent, to its original state as received from the origin server.

This response is intended to support a common data entry use case where the user receives content that supports data entry

Community
  • 1
  • 1
VoiceOfUnreason
  • 52,766
  • 5
  • 49
  • 91