I'm having some trouble passing a variable from JavaScript to smarty.
Example:
{literal}
<script type="text/javascript">
var js_variable = 110;
</script>
{/literal}
jQuery('div.fakbox_msg').html("{/literal}{lang_sprintf id=100013 1=js_variable}{literal}");
You cannot do that in an easy way. PHP, and by extension Smarty, is parsed and run on the server side before the browser get the data. JavaScript is run on the client side when the browser parses the HTML, CSS and Javascript that the server sent.
You will have to make a new HTTP request in some way, sending the new data. You can do that by reloading the entire web page and sending stuff in the querystring (after ? in the URL), or slightly more advanced by doing an Ajax call from your JS code and make JS do the changes of the page that you desire. The latter is more complex and requires some knowledge of Javascript, but the page does not need to be reloaded in its entirety.
for more info :
Assign JavaScript variable to Smarty variable
I never used Smarty, so I may be wrong, but from what I see on your code. this should work:
jQuery('div.fakbox_msg').html("{/literal}{lang_sprintf id=100013 1=" + js_variable + "}{literal}");
Smarty cannot use client side variables, such as the ones created by JavaScript.
Related
I have a script tag in a JSP and in that, I am reading a server side variable which I have read from the session.
I need to use a javascript variable (campaignIndex) in that server side variable ( getCreditAmountMax) like so:
<script>
var campaignIndex = jq111("select#campaigns").find(':selected').index();
GT.utilities.updateData(creditAmountDiv, "maxAmount", '<%=creditCampaignsModel.getCreditCampaigns().get(campaignIndex).getCreditAmountMax()%>', false);
</script>
Can I do that?
It is possible to dynamically generate Javascript code in JSP. It is not possible that JSP-expressions get reevaluated at client. From the point of view of the Javascript code it's only a constant.
Similar question: Access to Java model List from Javascript/JQuery dynamically.
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 6 years ago.
I have a variable set in the PHP with some relevant information that I need to process with the Javascript, but I can't figure out how to port that information over to the Javascript.
I first create a test variable in one javascript file, x
Test='<--!TestString-->'
In the PHP I use str_replace to alter the value of the variable with the relevant data
str_replace('<--!TestString-->',seralize($data), x)
Now I try to see if the data has been added in a different file using alert(Test)
and it shows me <--!TestString-->
Is there a better way to do this and have the data show with the alert function so I can start processing it?
I don't understand your approach at all.
Of course the ideal way to accomplish is to use AJAX to grab data from the server and get into the client side so you can work with it in Javascript. (You can Google "Ajax php and javascript")
The easiest way to accomplish this is to just print the variable in PHP but in a context that it will be available to JS in the client.
<html>
<body>
<script>
var x = <?php echo json_encode($phpvar); ?>;
console.log(x);
</script>
</body>
</html>
I won't argue with anyone that disapproves of that approach but its easy and it works.
Use <script> tag?
<script> my_value = 'some'; </script>
Use AJAX?
// with jQuery:
$.get('/data.php',function(data){/*...*/})
PHP is executed on your server, Javascript in the client (ie. Browser of the user visiting your website). So PHP can not change variables of your JS code once the page has been sent to the browser. But what you can do is set the variable in PHP and insert it in your JS code like this:
<?php
$test="some important info here";
?>
<script>
alert("<?php echo $test ?>");
</script>
If you need to change the variable by the server, you would need to use AJAX to load the new value.
As mentioned before we might not be understanding your approach to your problem.
PHP is executed on the server side while Javascript in the client. PHP cannot change variables of your Javascript code once the page has been sent to the browser. What must be done is to invoke PHP by using preferably AJAX with jQuery.
Look into AJAX PHP with JSON. Perhaps you are looking into the json_encode(value) function?
Ello there,
I'm trying to assign the value of a javascript variable to a java variable. But I don't have clue how to do this? Say for example I have this:
<html>
<head>
<script type="text/javascript">
function return variable(){
var a = "hello";
return a;
}
</script>
</head>
<body>
<%
//The java code
String b = //how do I get that javascript variable here?
%>
</body>
</html>
Java script plays on browser where java code is server side thing so you can't simply do this.
What you can do is submit the calculated variable from javascript to server by form-submission, or using URL parameter or using AJAX calls and then you can make it available on server
HTML
<input type="hidden" id="hiddenField"/>
make sure this fields lays under <form>
Javascript
document.getElementById("hiddenField").value=yourCalculatedVariable;
on server you would get this as a part of request
You need to read something about a JSP's lifecycle. Try this: http://en.wikipedia.org/wiki/File:JSPLife.png
JavaScript runs on the client, but in order to change the jsp, you need access to the server. This can be done through Ajax(http://en.wikipedia.org/wiki/Ajax_%28programming%29).
Here are some Ajax-related links: http://www.openjs.com/articles/ajax_xmlhttp_using_post.php
http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first
The answer is You can't. Java (in your case JSP) is a server-side scripting language, which means that it is compiled and executed before all javascript code. You can assign javascript variables to JSP variables but not the other way around. If possible, you can have the variable appear in a QueryString or pass it via a form (through a hidden field), post it and extract the variable through JSP that way. But this would require resubmitting the page.
Hope this helps.
JavaScript is fired on client side and JSP is on server-side. So I can say that it is impossible.
I think there's no way to do that, unless you pass the value of the JavaScript var on the URL, but it's a ugly workaround.
you cant do it.. because jsp is compiled and converted into html server side whereas javascript is executed on client side.
you may set the value to a hidden html element and send to servlet in request just in case you want to use for further
As JavaScript is client side and JSP is Server side.
So Javascript does not execute until it gets to the browser,
But Java executes on the server.
So, Java does not know the value of the JavaScript variable.
However you assign value of Java variable to JavaScript variable.
I am creating an HTML file for use with OpenWrt LuCI web interface. As discussed here: http://luci.subsignal.org/trac/wiki/Documentation/Templates I am using the Lua Markup language to run a Lua function called runDiag and I need to pass the javascript variable option to the runDiag function. I can't figure out how to make this work. I have tried various modifications to the markup inside the displayDiag function without success.
Can anyone help?
Example:
<%-
function runDiag(option)
return option
end
-%>
<script>
function displayDiag() {
var option = document.getElementById('iface').value;
document.getElementById('diag_text').innerHTML = '<%- write (runDiag(option)) -%>';
}
</script>
You can't do this. The Lua template is ran on the server, and the JavaScript code is ran on the client (i.e. web browser). They can't communicate.
The Lua code simply generates an HTML file to send to the client. It doesn't know about JavaScript; it's just some text that it's giving to the client. Here, option refers to a nonexistant Lua variable, which has the value of nil.
Conversely, the JavaScript code has no knowledge of the server-side Lua code. It just gets whatever the server generated. Thus, it only sees this line:
document.getElementById('diag_text').innerHTML = 'nil';
To communicate with the web server, you will need use AJAX or some other protocol.
For example, having:
<script type="text/javascript"
src="http://somedomain.com/js/somejs.js?14">
</script>
So what does "?14" means here?
Its a url param like any other parameter passed in a url. Sometimes JS scripts are created on the fly using server side technologies other times it is simply a version number to help with browser caching issues.
They are there to fool browsers into thinking that it is a new file.
This is a trick to avoid browser-cached copy when you update the JS file.
It means a variable is being passed to the script via GET, though standard JavaScript files don't support any means of collecting the variable.
You could, however, write a server script in PHP or ASP.NET that sets the content type as application/x-javascript.
Like this in php:
// file: external.php
<?php header("content-type: application/x-javascript"); ?>
// regular javascript here that uses $_GET['variable'];
Then you could put this in your HTML script tag:
<script type="text/javascript" src="external.php?variable=14"></script>
The javascript script is probably generated by a server side script (PHP, CGI, etc.) , which takes 14 as a parameter.
This is a query parameter as the browser will make an http get request to the somedomain.com for the javascript source.
If you load the page with a header browser like fiddler, you will see exactly what's going on.
IMHO, a JavaScript source like this will request "dynamic" content from server, thus the server will not try to use cached version of JavaScript file. Whether or not the parameter really does matter is up to the server.