简体   繁体   中英

How do I send form-data to a local MYSQL database?

I've made this form where the user can insert some information, and I want to send this to a local Mysql server I have running. It likely involves some javascript but i'm just not exactly sure what. The server is also connected via a REST JPA backend with some endpoints, where I have an endpoint for creating a new entity. Just not exactly sure how to utilize it in the frontend, if that is even what i'm supposed to do.

Here is my form:

<form action="http://localhost:8080/api/products/addProduct" method="POST">
                        <table class="formTable">
                            <tr>
                                <td><label for=pName">Product name:</label></td>
                                <td><input type="text" id="pName" name="name" required></td>
                            </tr>
                            <tr>
                                <td><label for="pPrice">Product price:</label></td>
                                <td><input type="number" id="pPrice" name="price" required></td>
                            </tr>
                            <tr>
                                <td><label for="pWeight">Product weight (gram):</label></td>
                                <td><input type="number" id="pWeight" name="weight" required></td>
                            </tr>
                        </table>
                            <input type="submit">
                    </form>

I put the form-action as my REST endpoint but i've got no clue if that's correct or not.

Yeah it's correct, yourfront_end is OK, at the backend you need to read the data through the endpoint on the action attribute on the form. Depending on which server side language you're using, for Express and Node.js the posted data can be found in the

request.body()

And on Django,

request.POST()

And after getting the data, you'll probably validate it against your business rules Before storing it on DB. Note that you'll need mysql drivers for that particular serverside language which can be found on the docs of the server side framework that's youre using...

I hope this answers your question...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM