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.
Related
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
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
I know similar questions have been asked and answered before, but I see so many different answers that I'm wondering if somebody can give an answer for my specific issue.
I wish to display a list of Soccer players on a webpage, which are retrieved from a database. Each player needs a drop-down box, allowing him to be picked/unpicked/subbed in the squad. When this happens, the database needs to be updated, along with the webpage, to show his new position and to store the information. I know how to update the database with PHP, but the only way that I know how to do the webpage part, is with Javascript. I thought I'd cracked it when I fathomed out how to use 'onchange' to call a Javascript function, but that's brought up another problem. If I pass the ID of the player and his new position to the function, I'm unable to update the PHP object from inside the Javascript, because there's no way (that I know of) for the PHP code to read the passed variables. What I'd want to do inside the function, is take the passed ID parameter, use that to match up the player in the PHP array, then update him.
The only solutions that I can think of are either
A) Don't use PHP at all in the HTML page and instead pass the information to the page with json_encode... presumably then read it into a Javascript array/function. Then when the function updates the player, call an PHP file (can parameters be passed into the file this way?).
B) Somehow convert the parameters in the Javascript function to PHP. Is this possible?
C) Find another way to respond to the down-down box that calls PHP instead of Javascript. Again, not sure if this is possible.
D) Call a PHP function from within the Javascript function. Again, don't think it's possible?
Is there a simpler solution or do I go with one of the above?
Javascript should be used to enhance the user experience. It should not be mandatory for your page to work.
First do your page with HTML and PHP. HTML passes your form data via post to your PHP script. PHP then populates your database with that data.
Then add javascript if you want to enhance functionality.
With javascript you can do things like validate the form so that you don't need to send the page to PHP until is it properly validated thus by saving you a page load. Note: You should still validate using PHP afterwards, javascript is only for enhancement.
You can pass the data from javascript to PHP using ajax.
I'm working on a Spring MVC app, which should let user alter contents of a list.
The list consists of Book-objects with simple properties like name and author. The view is a JSP page that displays the list of books and lets the user alter the contents.
Altering the list can mean adding books, removing them or changing the order of the books in the list.
Question is, how do I get the altered list back to the server? I can write JavaScript to control the list, but how do I post it to the Spring controller? On the other hand I can write a JSP form for altering the model which would be trivial to submit back to the server, but am I then limited to basic text fields in form input?
EDIT:
In JSP it is very easy to alter a single model's properties using a form like
<form:form action="myaction" method="post" commandName="mybook">
but if your model is a (ordered) list of objects then how do you edit it?
In Javascript I can get the list of objects from the response and change it as needed, but how do I submit it back to the server? Something like
$.post("/modifybook.do",{ name: "Spring in Action", author: "Graig Walls" } );
works, but only for single objects.
Having never used Spring this may well not be appropriate but could you not convert the list into XML and then post that back to Java and then work on it retaining all intricacies and changes?
You should avoid manipulating the whole list, period. I can't see a scenario where populating entire list of items and sending it back to the server is desired, when only one list element is being changed (edited, added or removed).
What I usually do in my app is I create a handler (controller) for returning a whole list of objects in one go and then add another handlers for adding, editing and removing single entries within that list. I also try to stick to REST in such scenarios, so that I have a clean API which represents server resources and the frontend (AJAX + jQuery) makes use of it.
This solution works really well for me so I suggest sticking to it as well.
Hi I have a javascript array of Url's in content.js file. I need to display the urls in seperate input boxes on a seperate page say display.php. Edit and delete buttons are given along with each input boxes. any change made by the user should be reflected in the content.js file.
I have shown all the url's in the input boxes. now an onclick event calls a function on button press.
My question is what will be the best way to update the array in content.js file(the file contains only one array). I'm trying to use file handling but that will require the content.js to have a strict format always. Is there a better way to modify the contents of the array or delete rows from array without file handling?
In first step, you should read the contents of JS file and pass it to json_decode function. It will give you PHP array. Use this array to display text boxes to user.
In second step, you should get the strings the user was submitted (e.g. from $_POST) and construct a PHP array from them. Then use json_encode to convert PHP array to JavaScript array represented in a string. Then you can write this string to JS file. So its content will be always valid.
You could do it with a server-side language like PHP. See this tutorial on how to write files using PHP. I would convert the array into a string using json_encode and use json_decode to convert it back into an array after reading it.
If you're only saving these URLs for the user and nobody else (so only the person entering/editing the data needs to see it) then I recommend using HTML5 localStorage