-3

I'm building an E-commerce website, in this process, I'm facing a problem that how to send users input data from javascript to the backend.

For the backend, I'm using node.js (express).

if I get the solution for this problem, then I can store the data in the database

                .mb-3
                    label.form-label(for='username') User name
                    input#username.form-control(type='text' aria-describedby='emailHelp')
                .mb-3
                    label.form-label(for='Email') Email
                    input#email.form-control(type='Email')
                .mb-3
                    label.form-label(for='mobilenumber') Enter number
                    input#mobilenumber.form-control(type='number')
                .mb-3
                    label.form-label(for='password') Enter New Password
                    input#password.form-control(type='password')
                .mb-3
                    label.form-label(for='confirmpassword') Confirm Password
                    input#confirmpassword.form-control(type='password')
                .form-check.login-checkbox-container
                    input.bg-danger.border-danger#t-and-c-checkbox.form-check-input(type='checkbox' checked)
                    label.form-check-label.m-0(for='exampleCheck1') Agree To Our 
                      a.text-danger Terms And Conditions
                .form-check.login-checkbox-container
                    input.border-danger.form-check-input#upcoming-notification(type='checkbox')
                    label.form-check-label.m-0(for='exampleCheck1') recieve upcomimg offers and events mails
                button.btn.btn-success#new-user-submit-btn(type='button') Submit
                button.btn.btn-outline-primary#signups-login-btn(type='button') Login

Any solution for this problem

Shyam
  • 17
  • 1
  • 4
  • Please provide more useful information. How do you send the data to your backend? How does the backend look? What errors are you facing? Just providing your HTML is not enough to get an answer. – Daniel Stoyanoff Dec 21 '21 at 06:16
  • Submit the form to the server? – Michel Dec 21 '21 at 06:47

1 Answers1

0

Use Ajax to send a User input data in Node server


I am give example of code to send a user input data to node server. use Jquery and Mongodb and nodejs. It is a simple Login page to Authentication.

Client Code:

 <script>
        function login() {
            var name = $("#name").val();
            var password = $('#password').val();
            if (name !== '' && password !== '') {
                $.ajax({
                    url: "http://localhost:3000/login",
                    type: 'POST',
                    crossDomain: true,
                    dataType: 'json',
                    data: {
                        name: String(name),
                        password: password
                    },
                    success: function(response) {
                        console.log(response.msg)
                        if (response.msg == 'verified') {
                            window.location.href = response.link;
                        } else {
                            alert(response.msg)
                        }
                    },
                    error: function(error) {
                        console.log(error);
                    }
                });
            } else {
                alert('Please Enter UserName Password')
            }
        }
    </script>

Server code

    var app = require('express')();
var bodyParser = require('body-parser');
var http = require('http').Server(app);
var port = 3000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017";

app.get('/', function(req, res) {
    res.sendFile(__dirname + '/index.html');
})

app.post('/login', function(req, res) {
    var data = req.body;
    var name = data.name;
    var password = data.password;
    MongoClient.connect(url, function(err, db) {
        if (err) throw err;
        let dbo = db.db('demo-mrracer');
        var query = { name: name }
        dbo.collection('user_details').find(query).toArray(function(err1, result) {
            if (err1) throw err1;
            if (result.length > 0) {
                var d = result[0].password;
                if (d == password) {
                    console.log('user verified..');
                    db.close();
                } else {
                    db.close();
                    res.send({ msg: 'Username or Password incorrect' })

                }
            } else {
                db.close();
                res.send({ msg: 'Please Register..' })
            }
        })
    })
})


app.listen(port, function() {
    console.log(`${port} Running Successfully..`)
})

That's it.

SarathKumar
  • 51
  • 1
  • 1
  • 11