how to pass the date variable into erb puts command
function myDayClickHandler(eventObj){
var date = eventObj.data.calDayDate;
<%puts "asdf",date%>
};
Javascript and Ruby are something very different. Javascript is client-side, running in the browser. Ruby is run on the server side, separately from client-side code.
Ruby code (<% .. %> parts in your example) is run on the server.
Javascript code (var date = eventObj.data.calDayDate;) is executed in the browser. It's also executed after the Ruby code, when server has processed request and generated result page already.
If you want to know value of date on the server, you'll have to send it from the browser to the server in AJAX request. Any popular Javascript library will help with that.
To pass data to the Rails controller from Javascript, you will need to pass the data back through attaching query parameters in the url.
http://www.google.com?variable1=1&variable2=2
Then in the rails controller you access the variable using
params[:variable1] or params[:variable2]
You can use custom controller actions to perform an activity either using javascript to submit or render, or use traditional HTML. Javascript would be preferred for user experience.
Related
I have a javascript Arraylist like this:
var javaScriptArray = $('#sortable').sortable("toArray"); // array of sortable elements
And I want to send this javascript Arraylist to java arrayList in the same jsp page
<% List <String> javaList ;
javaList = javaScriptArray;
%>
How can I do that?
I think you are doing it in a wrong way. As I have understand from your description and I want to send this javascript Arraylist to java arrayList in the same jsp page you want to pass a value from javascript directly to your jsp variable that is on the same page.
Always remember, JSP(JSF, other)/Pearl/Python/PHP and a like were server script(Server side) while Javascript is a client script(Client side).
This mean that you can pass the value from the server script to client script(Not a good practice) BEFORE the response/page were rendered to the client using <%= "Hello" %> or similar. But you CANNOT pass the value from the client to server which have ALREADY rendered the page to the client where the server is not aware of what is happening on the client.
It is like, when you order pizza from phone, the one that does the packing knows exactly what are on the box. But He/she will not know what will be added to it once it reaches you(the one who ordered it).
And to address your problem of passing of value from client to server. You have to pass the value from client to server using GET, POST, and other HTTP methods. This way, the server will be notified that the value of JavaScript changed. This is only good in passing value to notify the server to save/update/delete a record from the server.
This image will be helpful on how Client/Server interact to each other.
I create a Js Application and want to have English and German locals, which i can switch via button.
Is there a way to insert locals from a extra file in a .js file, like the function t'...' in Rails ?
As far as i know there is no way to do it directly and the reason is fairly simple too, erb is executed at the server side and javascript is a client side language which means its executed in your local browser, thats why if you even try to pass a variable between the two you'll have to make a request to the server, However this problem is tackled by calling an AJAX request, this AJAX request does the same thing as sending a new request to the server however it does that without refreshing or reloading the page to it gives the users the illusion that no request was made.
a guy asks a similar question here:
http://www.quora.com/Ruby-on-Rails/Can-I-pass-a-JavaScript-variable-to-a-Rails-method
and you can learn more about AJAX here:
http://www.w3schools.com/ajax/default.asp
I want store clickede_id value into $id2[] give me some suggestions and also suggest some advance details
function yes(clicked_id)
{
var it1=clicked_id;
alert(it1);
var tt1=1;
var tt2= "<?php echo($id2[var it1]); ?>";
//var tt2=document.getElementById("idcheck").value;
alert(tt2);
var tt3=document.getElementById("idcheck1").value;
//alert(tt3);
}
When you develop a web application, you are creating tools to let client and server communicates (over the HTTP protocol). This communication is based on Requests and Responses.
The client send request to server and the server responds with a reponse. In your case, you choosed PHP as the server-side language that will create your responses as answers to client request. Thes responses are HTML (+ javascript). Anyways, the reponses are static stuff to be interpredted by the client.
So the code you have sent is seen by the browser as:
function yes(clicked_id)
{
var it1=clicked_id;
alert(it1);
var tt1=1;
var tt2= 3; // or whatever value returned by php
var tt2=document.getElementById("idcheck").value;
var tt3=document.getElementById("idcheck1").value;
// ajax call here
}
When you say : store data from javascript to php (even if it doesnt look as a correct senetence), you mean sending data from client to server. It can be done via a classical Post request (via form submit with page refresh) or via ajax without page refresh.
For ajax, please to check jQuery documentation for $.ajax function (if you want to have a cross browser compatible solution), or XMLHTTPRequest object if you want raw javascript and do the cross-browser compatibility yourself.
PHP executes in the server and send the result to your browser to display. Your JS executes at this browser stage. What you need to understand is that PHP has already been finished it's execution when your JS gets a chance to execute. Trying to change something in PHP through the JS is trying to access the past.
But, the good news is, that you can adopt a model where you feed your JS through the PHP script (look at this echo "<script>var s = 'from php'</script>") and JS feeds your NEXT php execution. You can use ajax or direct page calling for this.
Probably you should read this question: How to pass data from Javascript to PHP and vice versa?
I have to call a javascript function from my Model or Controller. I can call it from the view file but how can I call a JS function directly from Model.
Is there a way to do that?
PHP is a server side language. This means the PHP file will get processed on the HTTP server and then deliver the generated HTML page to the client.
Javascript (JS) is interpreted on the the client (browser) and interfaces with the HTML and other remote resources.
Due to this model, PHP and JS know nothing about each other. So to call JS directly from the PHP file is not possible.
If you are looking to make a client browser call a JS function and the PHP script defines the values for the JS function you can use echo() and some string concatenation
//this is an example and will not run.
echo "<script>myJsFunction('" . $arg1 . "','" . $arg2 . "')</script>";
This will embed the JS function call in the HTML page returned to the client. Once the client renders the HTML page it will execute the function call as it comes across it.
If you could give an example of what you trying to do I might be able to give you a more accurate answer.
For a deeper explanation of the client server architecture and where things get executed have a look at Web Browser Client Server Architecture
No, try understanding the MVC pattern, the idea is that the model is not aware of the view and doesn't have to be. Also you can't call JS with php, which is a server side language and doesn't know anything about the client side scripts.
What you want is to implement long polling. I recommend you to learn and read about the basic technologies involved before trying to implement something.
http://en.wikipedia.org/wiki/Push_technology
http://en.wikipedia.org/wiki/Comet_%28programming%29
I have something like the below code. I need to pass the variable selectedIndex to the JSTL code. How can I do this?
function updateSP(selectedIndex)
{
<c:if test="${entry.key eq IC.oList[selectedIndex]}">
}
First, you need the following concept right: Java/JSP runs at the server machine and produces a HTML/CSS/JS page. The server machine sends HTML/CSS/JS page over network (HTTP) to the client machine. Client machine retrieves HTML/CSS/JS and starts to interpret HTML to display a markup structure, apply CSS to style and position the structure and execute JS on the resulting the HTML/CSS.
There is no means of any line of Java/JSP code at the client machine. Rightclick page and view source. The only way to pass Java/JSP variables to Javascript is to just output them as if it's a Javascript variable so that it has instant access to it once it runs at the client machine. The only way to pass Javascript variables to Java/JSP is to just send a HTTP request with that variable as parameter and have Java/JSP to listen on that specific request.
More background information and code examples can be found in this article.
jstl is executed in server side so you can't pass a javascript variable to jstl.
What you can do is generate dynamic javascript using jstl.
Please use the tool to format your code.