Send a javascript var to my model in FTL - javascript

I have a list of checkboxes, with this function i get my array of checked items:
var selected = new Array();
$('#bloqCountries input:checked').each(function() { selected.push($(this).val()); });
I have a model class and i need to send by parameter this array making something like this:
[#assign searchResults =model.searchContent(selected) /]
But Javascript is in client side and Freemarker in server side...
Then i don´t know how to get this array in my model to call my modelClass..
My idea is making by the request, transforming the array in string and making a split() method in java to delete the "-" between the words
I don't know how to send this content because i´m not using ajax and I don't think its necessary.

Simply put, you can't do that. FTL template is executed on the server and only once, to produce HTML and to send it to browser. JS is executed only when browser receives and processes said HTML. By that time, FTL process is long dead.
If you want to communicate with the server, ajax is the only way to do that. You might find some inspiration and examples of using ajax w/ Magnolia at here or here.
HTH,
Jan

Related

Using JavaScript function result in PHP

Is it possible to use the results of a JavaScript function in PHP?
Let me break down why. I have a table that has several columns, including a column that gives each row a checkbox. When a user presses an edit button, an onclick event is activated. The onclick event does a few things, but primarily it goes through the table and figures out which checkboxes are clicked, and returns an array of numbers which indicate the IDs of the entries of the selected rows.
What I want to know is how can I take these Ids and use them in my inline php code that should use the Ids to get corresponding data from a database and generate a new table.
Also, is there another way of doing this type of thing? This seems a little convoluted. I've tried XMLHTTPRequests to get the data directly with Javascript but I had trouble/couldn't get past using the GET method of an XMLHTTPRequest. My code would execute, but I wasn't sure how to get/use the data.
Any help is appreciated.
You can't use javascript values in your PHP statements because, imagine that the javascript is a client-side language, so it don't communicate with the server, and imagine that PHP is a server-side language, so it can't communicate with javascript. When you request a webpage, the request go to the server and the server will trait your request, and all output of this request (the webpage) is a code on client-side language and it is rendered with javascript, html and css. So, we can say that javascript have no idea of what is PHP, because, what PHP do is write a full html code and send to your browser render like a response. But you can use Ajax inside your client-side, there is a lot of tools developed in javascript that do the work of make requests to server-sides

Want to pass ArrayList contain class objects in javascript

I have a ArrayList which contain objects of a Java Class.I want to pass that list to javascript and want to populate the data within the object in to jsp.
The ArrayList is present in the jsp.Each class object only contain 2 parameter values.Class name is 'Certificate' and it contain 'certId' and 'certName'
jsp:
<%
ArrayList getAllCertificateNamewithId=new ArrayList();
getAllCertificateNamewithId=(ArrayList)mainMap.get("getAllCertificateNamewithIdMap");%>
<input type="button" width="16" height="16" onclick="fetchAndShow('<%=getAllCertificateNamewithId %>')" />
Javascript
function fetchAndShow(certObj){
//How to fetch the list's each object and cast to java class 'Certificate'
}
List contain
[com.dto.Certificate#9b44a1, com.dto.Certificate#1b6b3ac]
Think of it this way: you use Java + JSP as a means to write a text file that you then send to the user's browser, with JSP being a simple template mechanism.
You could theory implement your own method to serialize the List with objects to a String, and then implement a mechanism on the client side to deserialize into whatever data structure you want. For instance you could write your List as JSON.
But now I am inclined to ask: do you really, really, Really, REALLY need to do this ? Exposing server certificates of any kind to the client is a big no-no.
That's not the correct way. Just because of the contents are in the same page, they can't communicate directly.
Java plays on server side and javascript on server side. A client doesn't execute the java code.
To send a Java value to javascript you have to make a server request, So the server process your request and send the results back to Javascript.
Inshort you need to make a server (AJAX) request.
I don't know whether it would be right or not but it's worth a try. Try to use GSON or JACKSON libraries to convert your java object to JSON string. Then I think that you would be able to use that JSON String data in your Javascript code and get the certID and certName wherever you want.

How can i assign javascript array to Ftllist?

i have a list of values in javascript and the variable name is array with the values
['1','2','3'].here i want to assign this array values into a list in ftl file called list.
actually am trying to get checkbox value through js and assigning to a list which is the ftl file.
help me to overcome this issue.
FreeMarker runs on the server before the JavaScript runs on the client, so what you want is impossible. Or at least the way you want to do it, it is. You had to post that array back to the server (with a form or with AJAX), etc.
You have to send the list of values via ajax call and refresh a div to show new results.
since freemarker is a templating engine that runs on server to generate the html pages you will not be able to send any objects,lists, etc... to freemarker I could show you an example if you want.

Using Pylons global variables with JavaScript (escaping brackets)

I am trying to access a result in a dictionary held in a Python global variable within JavaScript.
var selected = jQuery('.target option:selected').text()
var list = "${c.persons_by_permission["+selected+"]}"
If I directly access the dictionary without the using the variable:
var list = "${c.persons_by_permission['stringID']}"
Then the code works.
However, when I am trying to use the variable to access the dictionary I get a syntax error (I believe its because of the curly braces).
I have tried escaping the braces with:
var list = "${{c.persons_by_permission["+selected+"]}}"
But it gives more syntax exceptions.
Any ideas?
EDIT:
This is not an issue of whether I can use Python and javascript together, it is more of a question of escaping the curly brackets
I am using pylons, so if I try:
alert("${c.persons_by_permission['Documents']}")
I will get the results I need. The issue is that I cannot find a way of passing a variable into the javascript in place of 'Documents'
I'm making a few (reasonable) assumptions here…
Your Python is running on your webserver
Your JavaScript is running on your client
As far as the Python is concerned it is outputting text. It doesn't care that the browser might interpret it as HTML, JavaScript or whatever. It just generates some text and then sends it to the browser.
With HTTP you get to make a request and have a response. You cannot stop a response half way through to get data back from the client.
You cannot pass data from client side JS back to the Python process that generated the JS. That process will have finished running.
You can either:
Send all the data to the client in the first place (and then select the bits you want in JS instead of in Python)
Have the JavaScript make a new HTTP request to the server and process the response in JS (this is known as Ajax and there are no shortage of tutorials out there for this)
I don't know what execution environment you are using,but I am almost certain that python and javascript are not executing at the same time.
${c.persons_by_permission['stringID']} looks to me like some kind of template directive. If so, this works because the directive is processed at template processing time. Your other form is just an expression in javascript that evaluates in javascript to a string.

Using jquery, what is the simplest function to post some json data and process a returned json response?

When users click on an element in my webpage, I would like to call a javascript function that reads the values of a few text boxes on the page, wraps their contents as json where the keys are the ids for the text boxes and the values are the contents of each text box, and then posts the resulting json to a url.
I would then like the same function to expect back a json response and call another javascript function with the returned json data.
Question:
What is the best way to write the javascript function to create a json structure from html elements, post the json with jquery, and call another javascript function with the resulting json response from the server?
I use malsup's jQuery form plugin.
http://malsup.com/jquery/form/
As he says himself "The jQuery Form Plugin allows you to easily and unobtrusively upgrade HTML forms to use AJAX."
There are some json related options in $.ajax()
Check them out: http://api.jquery.com/jQuery.ajax/
You can use json2 to marshall the data to json format and send it using ajax. Once you got the response back, you can use the result to call other javascript in your success callback. I would recommend using a decent javascript framework for ajax calls to make things a little bit easy.
Check out this question:
How can I post an array of string to ASP.NET MVC Controller without a form?
The accepted answer is pretty simple. But to make it even more simple, make postData a string, and have the SaveList method take in a single string. I just tried it and it works. Just a single statement on each the client and the server!

Categories