I have a dynamic form on one of my web pages. The form stores data in the script file and updates it as the user adds more data from the form. This is the object that I create in JS:
const survey = {
title: '',
createdBy: '',
description: '',
openingMsg: '',
thankyouMsg: '',
questions: [],
}
The questions array holds Question objects. Here is the class:
class Question {
constructor(id) {
this.id = id
}
questionText = ''
type = ''
choices = new Map()
}
I add an event listener to the submit button like this:
const saveSurveyBtn = document.getElementById('saveSurvey')
surveyForm.addEventListener('submit', saveSurvey)
and the saveSurvey function looks like this:
function saveSurvey(e) {
e.preventDefault()
fetch('../../actions/save-survey.php', {
method: 'POST',
body: JSON.stringify(survey),
headers: {
"Content-type": "application/json;charset=UTF-8"
}
})
}
As you can see I'm trying to send the data to a local php script from JS. All is well until I get to the php script. The problem is this; regardless of the fact that in my fetch call I use method: POST, it always gets sent as a GET request. Why? How can I fix this?