CMA Form with ChatGPT API Integration
/* Add your styling here */
Comparative Market Analysis (CMA) Form
async function fetchCMA() {
const address = document.getElementById("address").value;
const squareFootage = document.getElementById("squareFootage").value;
const lotSize = document.getElementById("lotSize").value;
const bedrooms = document.getElementById("bedrooms").value;
const bathrooms = document.getElementById("bathrooms").value;
// Show a loading message
const resultsDiv = document.getElementById("results");
resultsDiv.style.display = "block";
resultsDiv.innerHTML = "Fetching CMA data...";
try {
const response = await fetch("http://localhost:3000/cma", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
address: address,
squareFootage: squareFootage,
lotSize: lotSize,
bedrooms: bedrooms,
bathrooms: bathrooms
})
});
if (!response.ok) {
throw new Error('Error fetching data from server');
}
const data = await response.json();
if (data.choices && data.choices.length > 0) {
const averageMarketValue = data.choices[0].text.trim();
resultsDiv.innerHTML = `
Estimated Average Market Value: ${averageMarketValue}`;
} else {
throw new Error("Unexpected API response format");
}
} catch (error) {
// Show a user-friendly message and log the actual error for troubleshooting
resultsDiv.innerHTML = "Error fetching data. Please try again later.";
console.error("Fetch Error:", error.message || error);
}
}