I’m working on a web app with HTML and JavaScript for the front end and Node.js with MySQL for the back end. I’ve designed a form to collect user details such as name, email, address, and payment info. Although I have implemented a client-side email validation, I’m stuck on sending the data properly to the server and inserting it into the database.
Here’s a sample of the updated code:
function submitForm() {
const name = document.querySelector('#name').value;
const email = document.querySelector('#email').value;
const address = document.querySelector('#address').value;
const phone = document.querySelector('#phone').value;
const payment = document.querySelector('#payment').value;
function isValidEmail(email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
if (!isValidEmail(email)) {
alert('Please enter a valid email address');
return;
}
// TODO: Send data to server
}
Any assistance or advice on how to proceed would be much appreciated. Thanks!
hey there boldpainter37! have you considered using fetch() to send your form data to the server? it’s pretty simple to set up. also, what about server-side validation? that’s important too. curious to hear how you’re handling security for sensitive info like payment details. any plans for that?
To send the form data to your Node.js server, you can use the Fetch API. Begin by creating a POST request that sends your form data as JSON. Ensure you set the appropriate headers and handle the server’s response accordingly. On the server side, use Express to define a route that accepts the data and performs server-side validation. For inserting into the database, utilize prepared statements to prevent SQL injection. When dealing with payment information, it is advisable to integrate a secure gateway like Stripe or PayPal to manage the sensitive data safely.
yo boldpainter37, fetch() is def the way to go for sending data. but don’t forget bout server-side validation too - never trust client-side alone. for payment stuff, look into libraries like stripe. they handle the security headaches for ya. good luck with ur project!