0

So I have this code

//Admin Panel Page
router.get('/admin', ensureAuthenticated, async (req, res) => {
    if (req.user.admin !== true){
        req.flash('error_msg', 'You do not have access to reports, sorry.');
        res.render('dashboard', {
            name: req.user.name
        })
    }
    else{
        try {
            var adminArray = [];
            var html = "<link rel='stylesheet' href='/css/freelancer.css' type='text/css'/>" +
                        "<script src='https://code.jquery.com/jquery-3.4.1.slim.min.js' integrity='sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n' crossorigin='anonymous'></script>" +
                        "<nav class='navbar navbar-expand-lg bg-secondary text-uppercase fixed-top' id='mainNav'><div class='container'>" +
                        "<a class='navbar-brand js-scroll-trigger' href='/dashboard'>Chinook Winds Logistics LLC</a>" +
                        "<button class='navbar-toggler navbar-toggler-right text-uppercase font-weight-bold bg-primary text-white rounded' type='button' data-toggle='collapse' data-target='#navbarResponsive' aria-controls='navbarResponsive' aria-expanded='false' aria-label='Toggle navigation'>Menu<i class='fas fa-bars'></i></button>" +
                        "<div class='collapse navbar-collapse' id='navbarResponsive'><ul class='navbar-nav ml-auto'><li class='nav-item mx-0 mx-lg-1'><a href='/admin' class='btn btn-primary'>Admin Panel</a></li><li class='nav-item mx-0 mx-lg-1'>" +
                        "<a href='/reports' class='btn btn-primary'>Reports</a></li><li class='nav-item mx-0 mx-lg-1'><a href='/users/logout' class='btn btn-primary'>Logout</a>" +
                        "</li></ul></div></div></nav>< br/><body><script src='https://kit.fontawesome.com/4a2ea255d5.js' crossorigin='anonymous'></script><div class='container-blog'><table class='table-primary table-bordered table' style='border-spacing: 10px;'>"+
                        "<tr><th>Users Name</th><th>Email Address</th><th>Truck Driver</th><th>Truck Number</th><th>Admin</th><th>Customer</th><th>Load Planner</th><th>Deactivate Account?</th></tr><tr><form action='/admin' method='post'>";
            await User.find({isVerified: true}, function(err,resp){
                if(err){
                    console.log('Error Fetching Model');
                    console.log(err);
                }
                adminArray = resp;
                adminArray.forEach(function(data){
                    html += "<td>" + data.name + "</td>";
                    html += "<td>" + data.email + "</td>";
                    html += "<td><input type='hidden' name=truckDriver value=''><input class='form-control' name='truckDriver' type='checkbox' value=" + data.email + "></input></td>";
                    html += "<td><input class='form-control' name='truckNumber' type='number'></input></td>";
                    html += "<td><input type='hidden' name=admin value=''><input class='form-control' name='admin' type='checkbox' value=" + data.email + "></input></td>";
                    html += "<td><input type='hidden' name=customer value=''><input class='form-control' name='customer' type='checkbox' value=" + data.email + "></input></td>";
                    html += "<td><input type='hidden' name=loadPlanning value=''><input class='form-control' name='loadPlanning' type='checkbox' value=" + data.email + "></input></td>";
                    //html += "<td><input class='form-control' name='deactivate' type='checkbox' value=" + data.email + "></input></td>";
                    html += "</tr><tr>";
                });
                html += "</tr></table><input class='btn btn-primary' type='submit'></input></form></div></body>";
                html += "<script src='js/freelancer.js'></script>";
                html += '<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script><script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script><script src="public/js/freelancer.js"></script><script src="public/js/jqBootstrapValidation.js"></script><script src="public/js/contact_me.js"></script>';
                res.send(html);
            })
        }
        catch (e){
            res.send('error_msg');
            console.log(e);
        }
    }
});

This creates a table of all verified users. This...

// Admin Update Panel
router.post('/admin', ensureAuthenticated, async (req, res) => {
    try {
        const { truckDriver, truckNumber, admin, customer, loadPlanning } = req.body;
        await User.find({isVerified: true}, function(err,data){
            adminArray = data;
            console.log(req.body);
            q = 1;
            adminArray.forEach(async (data, res) =>{
                if(truckDriver[q] == data.email){
                    await User.findOneAndUpdate({email: data.email}, {truckDriver: true}, {useFindAndModify: false});
                }
                if(admin[q] == data.email){
                    await User.findOneAndUpdate({email: data.email}, {admin: true}, {useFindAndModify: false});
                }
                if(customer[q] == data.email){
                    await User.findOneAndUpdate({email: data.email}, {customer: true}, {useFindAndModify: false});
                }
                if(loadPlanning[q] == data.email){
                    console.log(yes);
                    await User.findOneAndUpdate({email: data.email}, {loadPlanning: true}, {useFindAndModify: false});
                }
                q = q + 2;
                console.log(q);
            });
        });
        req.flash('success_msg', 'Your accounts have been processed!');
        res.redirect('/admin');
    }
    catch (e){
        res.send('error_msg');
        console.log(e);
    };
});

Is supposed to iterate through the submitted form and update user accounts based on the previous form's data.

I am trying to make it so that if a checkbox is unchecked, the form will still pass a value in its place (can be null). I tried using Hidden fields to accomplish this, but when I hit submit, the form will post all the hidden checkboxes and all the checked checkboxes.

How can I make it so that either all unchecked checkboxes pass a null variable to POST, or continue to do it the way I am and remove the values of hidden checkboxes if the corresponding checkbox is checked?

  • The answer to your question is here https://stackoverflow.com/questions/1809494/post-unchecked-html-checkboxes - Now I would suggest using templates to make your code more readable, check this out https://ejs.co/ – ariel Mar 11 '20 at 04:01
  • Does this answer your question? [POST unchecked HTML checkboxes](https://stackoverflow.com/questions/1809494/post-unchecked-html-checkboxes) – ariel Mar 11 '20 at 04:02
  • So, I've tried following those posts, but they didn't help me resolve the issue. – The Traveling Coder Mar 11 '20 at 04:21
  • Change the HTML to a template and you will see better where is the error – ariel Mar 11 '20 at 06:51
  • I had that initially, but couldn't figure out how to dynamically build the table from the information in the database using a template. So, I coded it this way – The Traveling Coder Mar 12 '20 at 20:03
  • https://ejs.co/#docs – ariel Mar 12 '20 at 20:50

0 Answers0