'How to create a MySQL database connection in javascript of Knockout.js file?
I have created the following table in MYSQL and a file in Knockout.js.I want that the data we are entering in the Knockout.js through variable pf should be stored in the table "PORTFOLIO".The database connection I want to implement is through javascript only.I tried creating a database connection between MYSQL and Knockout.js but was not successful.Kindly help.
//TABLE
CREATE TABLE PORTFOLIO
(
ID INT NOT NULL,
pf VARCHAR (255)
);
File in Knockout.js
//View
<h3>Portfolio</h3>
<form data-bind="submit: addpf">
Add Portfolio: <input data-bind="value: newpf" placeholder="Who needs to be added?" />
<button type="submit">Add</button>
</form>
<ul data-bind="foreach: pf, visible: pf().length > 0">
<li>
<input type="checkbox" data-bind="checked: isDone" />
<input data-bind="value: title, disable: isDone" />
<a href="#" data-bind="click: $parent.removepf">Delete</a>
</li>
</ul>
You have <b data-bind="text: oldpf().length"> </b> New Portfolios
<span data-bind="visible: oldpf().length == 0"></span>
//ViewModel
function pf(data) {
this.title = ko.observable(data.title);
this.isDone = ko.observable(data.isDone);
}
function pfListViewModel() {
// Data
var self = this;
self.pf = ko.observableArray([]);
self.newpf = ko.observable();
self.oldpf = ko.computed(function() {
return ko.utils.arrayFilter(self.pf(), function(pf) { return !pf.isDone() });
});
// Operations
self.addpf = function() {
self.pf.push(new pf({ title: this.newpf() }));
self.newpf("");
};
self.removepf = function(pf) { self.pf.remove(pf) };
}
ko.applyBindings(new pfListViewModel());
Solution 1:[1]
No this is not possible, JavaScript can not directly connect to MySQL. But you can mix JS with PHP to do so or any other server side language.
JavaScript is a client-side language and your MySQL database is going
to be running on a server. This means you need to have a server side
language which allows them to speak. Then you can use AJAX to submit the data to the server.
Solution 2:[2]
Other option is that you can go with BreezeJs which will give you freedom to make Data-centric Application. You can take a look on this link http://breeze.github.io/doc-js/. Breeze works absolutely fine with knockoutJs.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | MT0 |
| Solution 2 | ashishraaj |
