I have a two files called sample.jade and another file called insertToEnrolledSubjects.js file,(and I'm using nodejs with express framework and for db i'm using mongo db)
sample.jade file displays a table with course details. By clicking Enroll, edit ,delete button in each row of the table students can enroll for courses,edit course or delete it. So there when i click Enroll button it shows a pop up message asking about confirmation when user clicks "ok" button i need to call "insertToEnrolledSubjects" function in insertToEnrolledSubjects.js file how can i do that? Please can anyone give me a better solution i tried a lot but i didn't get any result.
sample.jade
html
head
title="Enroll For Subjects"
link(rel='stylesheet', href='/bootstrap/css/bootstrap.min.css')
link(rel='stylesheet', href='/bootstrap/css/bootstrap-responsive.min.css')
script(src='https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js')
script(src='bootstrap/js/bootstrap.min.js')
script(src="insertToEnrolledSubjects.js")
body
div.container
div.content
h2.display-4.m-b-2 Course criteria
table.table.table(border='2')
thead
tr
th Course ID
th Subject name
th Year
th Credits
th Action
tbody
- for (var i = 1; i <= 5; ++i){
tr
td=i
td="IPE"
td=i
td=5
td
button.btn.btn-danger(type='submit',onclick='Enroll("#{i}")') Enroll
td
button.btn.btn-danger(type='submit',onclick='Edit("#{i}")') Edit
td
button.btn.btn-danger(type='submit',onclick='delete1("#{i}")') Delete
- }
script.
function delete1(d){
var val = confirm("Do you want to delete this record?");
if(val == true){
window.location.href= '/delete1/'+d;
return true;
}
else{
return false;
}
};
script.
function Enroll(d){
var val = confirm("Do you really want to enroll for this course?");
if(val == true){
val2 = confirm("came");
insertToEnrolledSubjects(d,1);
val3 = confirm("came3");
}};
insertToEnrolledSubjects.js
var MongoClient = require('mongodb').MongoClient
, format = require('util').format;
MongoClient.connect("mongodb://localhost:27017/test", function (err, db) {
if (err)
throw err;
else {
console.log("successfully connected to the database!!!");
//calling insert function
insertToEnrolledSubjects(cid,e);
//implementation of insert function
function insertToEnrolledSubjects(courseid,enrolled) {
db.collection('EnrolledSubjects').insert({ courseId:courseid ,NoOfEnrolledStudents:enrolled});
console.log("Inserted!");
//Show total no of reults in db
db.collection('EnrolledSubjects').count(function (err, count) {
if (err) throw err;
console.log("Total Rows: " + count);
});
}
}});
simple.jadewill be render to normal html file - it is client side, butinsertToEnrolledSubjectsis server side. In your case, you can make a http request to do callinsertToEnrolledSubjectswith 2 pramscourseidandenrolled.