DIDS-Coursework/DIS-COMP4039-CW2-psxrp11-20450011/psxrp11-20450011_InstallationFiles/js/newVehicle.js

67 lines
2.0 KiB
JavaScript

document.addEventListener("DOMContentLoaded", () =>
{
fetch("getOwners.php").then(res => res.json().then(json =>
{
if(json.message === "ok")
{
let body = "";
for (const owner of json.owners)
{
body += `<option value="${owner.People_ID}">${owner.People_name} - ${owner.People_licence}</option>`;
}
body += `<option value="new">New Owner</option>`;
document.querySelector("#owner").innerHTML = body;
}
}));
});
document.querySelector("#owner").addEventListener("change", e =>
{
let inputs = document.querySelectorAll("#addOwner input");
if (e.target.value === "new")
{
document.querySelector("#addOwner").classList.add("shown");
for (const input of inputs)
{
input.setAttribute("required", "");
}
}
else
{
document.querySelector("#addOwner").classList.remove("shown");
for (const input of inputs)
{
input.removeAttribute("required");
}
}
});
document.querySelector("#vehicleForm").addEventListener("submit", e =>
{
e.preventDefault();
let formData = new FormData();
formData.append("type", document.querySelector("#type").value);
formData.append("colour", document.querySelector("#colour").value);
formData.append("plateNum", document.querySelector("#plateNum").value);
if (document.querySelector("#owner").value === "new")
{
formData.append("name", document.querySelector("#name").value);
formData.append("address", document.querySelector("#address").value);
formData.append("licence", document.querySelector("#licence").value);
}
else
{
formData.append("peopleID", document.querySelector("#owner").value);
}
fetch("addVehicle.php", {
method: "POST",
body: formData
}).then(res => res.json().then(json =>
{
alert(json.message);
}));
});