Post query from client-side Javascript using Express (node.js) - javascript

I'm developing a web app using Express. On one of my pages I have a table filled with data and I want users to be able to modify the data. I use contenteditable (HTML5) to make the data editable (DEMO: http://jsfiddle.net/k854hsae/5/)
I have a Javascript method to submit whatever has been written when the Enter key is pressed:
$(document).ready(function () {
var $editableTd = $('td[contenteditable]');
$editableTd.bind('keyup', function (e) {
if (e.keyCode == 13) {
$(this).blur();
// SAVE NEW DATA TO DATABASE
}
});
});
I use MongoDB. I want to save the new data to the database every time a cell is modified (so no "submit button"). I'm not quite sure on how to pass the information along in Express. From the examples I've seen, they all use Forms with the following syntax in Jade:
form#formAddUser(name="adduser",method="post",action="/adduser")
But I don't know how I can POST using contenteditable. Is there a way to "call" server-side JS from client-side JS?

You can use jQuery .post() method to send ajax post request from client-side JS to server-side JS.

Related

Dynamic NAV Control Add-in exchange data

Im solving how to exchange data between JS control add-in and NAV.
Now, when I want to get data from JS control add-in to NAV. I call from NAV, JS method and in JS method I call method in NAV. See example below.
Is there some easy way e.g. return values on first call from NAV?
Because I need data from JS in one method.
Thank you for your help.
C/AL Code
d::someMethod()
//I need to work with data from JS here
CurrPage.d.getDataFromJS();
d::receiveDataFromJS(data: Variant)
//here I receive data from JS
JS
function getDataFromJS() {
var result = 'bla bla';
Microsoft.Dynamics.NAV.InvokeExtensibilityMethod('receiveDataFromJS', [result]);
}
You can return data from your addin via events. Just define the event in your dll and reinclude your addin, it should then be visible in the C/AL. To trigger the event on JavaScript side use Microsoft.Dynamics.NAV.InvokeExtensibilityMethod('eventName', [parameters]);
The parameters you parse here can then be used to parse your data to the NAV-side. I hope this helps you

How to post and get javascript document into Database

I've a web page in which i've inserted a ACE- javascript Code Editor. I want to post javascript code into my database and then retrieve.
for example in case of JSON we use json.stringify and json.parse to post and retrieve data. I'm usieng SAPUI5 backend is in javascript.
var conf = model.getProperty("/automation-rule-body");
Is there any rule to post javascript code into database ?
If your backend supports REST API use create method of sap.ui.model.odata.ODataModel
var oDataModel = sap.ui.model.odata.ODataModel(sServiceUrl, mParameters);
oDataModel.create(sPath, oData, mParameters);

Passing data from PHP to JS and JS to PHP

Hi I'm trying to learn PHP and javascript, therefore I tried to do an exercise about passing data but I didn't understand the js function and currently unable to do anything.
Here is the code
php-code.php
$v1=$_GET['v1'];
$v2=$_GET['v2'];
$v3=$_GET['v3'];
//Some operations about them
echo $output;
my-js.js
var v1 = $('#v1 option:selected').val();
var v2 = $('#v2 option:selected').val();
var v3 = $('#v3 option:selected').val();
//This is where I want to pass these variables to my php file and get the output
//But i don't know how
js-execute.php
//this is where my js gets the variables at first
PHP is a server-side language while JavaScript is a client-side one. This means that PHP will be executed when someone requests a page, but the browser will only get the result of the PHP code. On the other hand, JavaScript is sent to the browser and the browser will execute it at the appropriate time (when the page loads or when an event happens). That's why if you look at the source code of a page, you will be able to see the JavaScript code, but never the PHP code.
If you want to pass values from JavaScript to PHP, you will need to make a remote call to a PHP file. PHP isn't like JavaScript – once it's done running its code, it won't be able to respond to anything without a reload of the page.
The easiest way to send something to a PHP file and fetching the result with JavaScript can probably be achieved with JQuery. It has a function $.get which will fetch a given URL. Just be sure to properly validate the input on the server side – never trust user input.
JavaScript (using JQuery to send v1, v2 and v3 to page.php)
function requestPage(v1, v2, v3) {
$.get('page.php', {'v1':v1, 'v2':v2, 'v3':v3}, function(data) {
alert(data);
});
}
PHP (a trivial example)
// Be sure to properly validate input!
if(isset($_GET['v1']) && is_scalar($_GET['v1'])) {
echo strrev($_GET['v1']);
}

Using a jquery datatable with php mysql data - developing further

I've been exploring jquery datatables and flexigrid plug ins to display data from a mysql database.
I have both versions displaying data from the database which comes from a php script. So all ok there.
However I want to add a form to enable a search to made on criteria eg from and to dates, types etc. multiple critieria. Flexi grid has a basic filter - i've not delved too deeply with datatable.
I can do this without using a plug in but I would like my results to be displayed in the chosen datagrid and then use that tables functionality. I'm just not sure of the workflow on how to do this.
How do I initiate the creation of a flexigrid or datatable from the call back of a form submission with the results from that call back? Do I save the results in a variable and pass that to the plug ins?
I hope my question makes sense. I know what I would like to do, just not sure how to do it
I'm ok with php - newsih to javascript
Thanks
Flexigrid
You can create a form (e.g. call it searchForm) and then serialize this data and pass it into the Flexigrid search parameters.
You can then add form data to a Flexigrid implementation on the click of the button:
$(function () {
$('#btnSearch').click(function () {
addFormData();
// Reload the grid from the URL, passing in search parameters from addFormData
$('#flexGrid').flexOptions({ url: '/search/here/' }).flexReload();
});
});
And the addFormData function just serializes the data from the form and adds it to the parameters of the Flexigrid call back to the server:
function addFormData() {
//Retrieve all input data from the searchForm
var allSearchParams = $('#searchform').serializeArray();
//Add the parameters to Flexigrid, so when it reloads, it will use them to filter the data
$("#flexGrid").flexOptions({ params: allSearchParams });
return true;
}
DataTables
DataTables has handy plugins like the Column Filter which has server-side filtering to do all this work for you, or I would recommend reading up on DataTables server-side processing.

Authorize.Net CIM, After saving detail in hosted popup form, need to call my custom JavaScript function

When I save payment detail in CIM hosted popup form after saving data massage display like
'your information has been saved', I need to call at that time my java-script function, Is it possible?
This JavaScript should do the trick:
AuthorizeNetPopup.options.onPopupClosed = function() {
// your JavaScript code here
};

Categories

Resources