Learn how to send form data to your ML model backend with our easy step-by-step guide for seamless integration.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
name attributes, as these are used as keys when sending data.
// Example of a basic HTML form
<form id="mlForm">
<label for="userReview">User Review:</label>
<textarea id="userReview" name="userReview" required></textarea>
<input type="submit" value="Submit Review">
</form>
// JavaScript to handle form submission and send data to the ML backend
document.getElementById('mlForm').addEventListener('submit', function(event) {
event.preventDefault(); // Prevent form from submitting normally
// Create a FormData object from the form
const formData = new FormData(this);
// Convert formData to a simple object
const data = {};
formData.forEach((value, key) => {
data[key] = value;
});
// Send data to the backend using fetch
fetch('/api/predict', {
method: 'POST',
headers: {
'Content-Type': 'application/json' // Inform server of the JSON payload
},
body: JSON.stringify(data) // Convert the object to JSON string
})
.then(response => response.json())
.then(result => {
// Process result returned from the ML model
console.log('Prediction Result:', result);
})
.catch(error => {
// Handle errors from the request or processing
console.error('Error:', error);
});
});
/api/predict).
// Example using Express.js for the backend endpoint
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
// Parse JSON bodies from POST requests
app.use(bodyParser.json());
// Define the ML prediction endpoint
app.post('/api/predict', (req, res) => {
// Access the form data sent from the frontend
const { userReview } = req.body;
// Validate received data
if (!userReview) {
return res.status(400).json({ error: 'Review data is required' });
}
// Preprocess the input before feeding it to the ML model
// For example: convert to lower-case, strip unnecessary characters, etc.
const processedReview = userReview.toLowerCase().trim();
// Integrate your ML model for prediction/pipeline processing
// This could involve calling a locally stored model,
// or forwarding the request to a separate ML service.
// Example:
predictReview(processedReview)
.then(prediction => {
// Return prediction result to the frontend
res.json({ prediction });
})
.catch(error => {
// Handle any error during prediction
res.status(500).json({ error: 'Prediction failed' });
});
});
// Dummy prediction function to simulate ML model processing
function predictReview(review) {
return new Promise((resolve, reject) => {
// Simulate asynchronous prediction operation
// In practice, call your model here and resolve with the result
setTimeout(() => {
// Basic mock prediction: positive if includes 'good', negative otherwise
const sentiment = review.includes('good') ? 'positive' : 'negative';
resolve(sentiment);
}, 1000);
});
}
// Start the server on port 3000 or a specified port
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
prediction or error).
// Example snippet for handling the response on the frontend
fetch('/api/predict', { /_ code as above _/ })
.then(response => response.json())
.then(result => {
if (result.prediction) {
// Update the UI with the prediction
document.getElementById('result').innerText = 'Prediction: ' + result.prediction;
} else if (result.error) {
// Inform the user of an error
document.getElementById('result').innerText = 'Error: ' + result.error;
}
});
From startups to enterprises and everything in between, see for yourself our incredible impact.
Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We’ll discuss your project and provide a custom quote at no cost.Â