Skip to content
Advertisement

fetch send formdata and recieve it from php

Hello srry for bad english, i’m trying to insert some formdata to my sql database with fetch request, the row was added and the id counts +1, but theres no extra data added to the columns “nomtar”,”destar”,etc

JS function on submit form:

JavaScript

agregar.php FILE:

JavaScript

Advertisement

Answer

There’s no need to convert your form data to JSON. Fetch understands FormData directly.

Change this line

JavaScript

to this:

JavaScript

Now your PHP code can extract the incoming data from $_POST without having to read php://input

JavaScript

That should get you going, but be aware, your code is vulnerable to SQL injection. Once you get this fixed you should refactor your code to use prepared statements.

Note: FormData can extract data directly from a form without you extracting it field by field. let formData = new FormData(document.getElementById('formId')) will do it in one operation.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement