I am doing a CRUD which has interested parties and courses. I want that when editing the interested parties, it brings me the courses in which they are interested. These courses are shown with a select multiple but I don't know how to mark them as selected.
The query to the DB is through php, and I am doing all the functionality with JS.
I did the following but it doesn't work
this.editar = (id) => {
var form = new FormData();
form.append("id", id);
fetch("../controllers/editar.php", {
method: "POST",
body: form,
})
.then((res) => res.json())
.then((data) => {
document.getElementById("id_interesado").value = data.id_interesado;
document.getElementById("nombre").value = data.nombre;
document.getElementById("apellido").value = data.apellido;
document.getElementById("telefono").value = data.telefono;
document.getElementById("email").value = data.email;
document.getElementById("direccion").value = data.direccion;
document.getElementById("numero").value = data.numero;
document.getElementById("localidad").value = data.localidad;
document.getElementById("dni").value = data.dni;
var form_cursos = new FormData();
form_cursos.append("id_interesado", data.id_interesado);
fetch("../controllers/editar_cursos_interes.php", {
method: "POST",
body: form_cursos,
})
.then((res) => res.json())
.then((data) => {
var ids_cursos = Array();
data.forEach((item) => {
ids_cursos.push(item.id_curso);
})
ids_cursos.forEach((item) => {
if (document.getElementById("curso_interes").value in ids_cursos) {
document.getElementById("curso_interes").setAttribute("selected", true);
}
})
})
.catch((error) => console.log(error));
})
.catch((error) => console.log(error));
};
From what I understand what you want is to mark an item of a select as selected, being so you can do something simple like this:
For example:
Mama will be marked, you just have to have the information in the select and tell it the corresponding value .