I'm trying to follow this guide to update a form field when the user change another field.
I've correctly setup my FormTypes, but I'm having trouble submitting the form in Ajax without JQuery.
I have 2 select :
const blockchain = document.getElementById('strategy_farming_blockchain');
const dapp = document.getElementById('strategy_farming_dapp');
const csrf = document.getElementById('strategy_farming__token');
The blockchain
field is supposed to update the dapp
field.
If I submit the whole form, it's working :
blockchain.addEventListener('change', function () {
const form = this.closest('form');
const method = form.method;
const url = form.action;
var request = new XMLHttpRequest();
request.open(method, url, true);
request.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
request.onload = function () {
if (this.status >= 200 && this.status < 400) {
//Success
const html = new DOMParser().parseFromString(this.response, 'text/html');
dapp.innerHTML = html.querySelector('#strategy_farming_dapp').innerHTML;
} else {
//Error from server
console.log('Server error');
}
};
request.onerror = function () {
//Connection error
console.log('Connection error');
};
request.send(new FormData(form));
});
But I'm not supposed to submit the whole form, I'm supposed to submit only the blockchain
value
I tried a lot of things, like
var formdata = new FormData(form);
formdata.delete(dapp.name);
request.send(formdata);
// It's working for a new entity, but if I'm editing one, it's not updating the dapp field...
or
var formdata = new FormData();
formdata.append(this.name, this.value);
formdata.append(csrf.name, csrf.value);
request.send(formdata);
// It's working in a NEW action, but not in an EDIT action...
or
var data = {};
data[this.name] = this.value;
request.send(data);
//or
request.send(JSON.stringify(data));
//If I dump($request->request) in the controller, it seems like there's no data...
//Or the request isn't parsed correctly, or there's something missing ?
I also tried with encodeURIComponent
...
I'm out of ideas... Any ideas ? Thanks !