I have two halves of a JavaScript file which I would like to connect and I was wondering if someone might be able to help? I am looking to obtain the HTML form objects 'Title', 'Subtitle' and 'Body' from a HTML form. I then want to be able to pass these variables I have collected into the second half of the JavaScript file and post them into a SQL database.
$(document).ready(function () {
var Title = $('#Title').val();
var Subtitle = $('#Subtitle').val();
var Body = $('#Body').val();
}
$(document).ready(function() {
$(document).on('submit', 'form', function() {
// Here you get the values:
var Title = $('#Title').val();
var Subtitle = $('#Subtitle').val();
var Body = $('#Body').val();
// OR
// you have a simpler option:
// var data = this.serialize();
$.ajax({
type: 'POST',
data: {
Title: Title,
Subtitle: Subtitle,
Body: Body
},
// OR
// data: data, // if you use the form serialization above
url: "/adddata",
success: added,
error: showError
});
});
$("#submit").click(function() {
console.log(id);
$.ajax({
type: 'GET',
dataType: "json",
url: "/alldata",
success: showScores,
error: showError
});
});
});
var Connection = require('tedious').Connection;
var config = {
server: 'localhost', //update me
authentication: {
type: 'default',
options: {
userName: '', //update me
password: '' //update me
}
},
options: {
// If you are on Microsoft Azure, you need encryption:
encrypt: true,
trustServerCertificate: true,
database: 'Healthcare' //update me
}
};
var connection = new Connection(config);
connection.on('connect', function(err) {
if(err) {
console.log('Error: ', err)
} else {
// If no error, then good to proceed.
console.log("Connected");
executeStatement();
executeStatement1();
}
});
connection.connect();
var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;
function executeStatement() {
var request = new Request("SELECT * FROM dbo.Core;", function(err) {
if (err) {
console.log(err);}
});
var result = "";
request.on('row', function(columns) {
columns.forEach(function(column) {
if (column.value === null) {
console.log('NULL');
} else {
result+= column.value + " ";
}
});
console.log(result);
result ="";
});
request.on('done', function(rowCount, more) {
console.log(rowCount + ' rows returned');
});
// Close the connection after the final event emitted by the request, after the callback passes
request.on("requestCompleted", function (rowCount, more) {
connection.close();
});
connection.execSql(request);
}
var Request = require('tedious').Request
var TYPES = require('tedious').TYPES;
function executeStatement1() {
var request = new Request("INSERT dbo.Core (Title, Subtitle, Body) VALUES (@Title, @Subtitle, @Body);", function(err) {
if (err) {
console.log("Couldn't insert data" + err);}
});
request.addParameter('Title', TYPES.NVarChar,'This is a test title from Javascript');
request.addParameter('Subtitle', TYPES.NVarChar , 'This is a test subtitle from Javascript');
request.addParameter('Body', TYPES.NVarChar, 'This is a test body from Javascript');
request.on('row', function(columns) {
columns.forEach(function(column) {
if (column.value === null) {
console.log('NULL');
} else {
console.log("Product id of inserted item is " + column.value);
}
});
});
// Close the connection after the final event emitted by the request, after the callback passes
request.on("requestCompleted", function (rowCount, more) {
connection.close();
});
connection.execSql(request);
}