How to access JSP Map in vanilla Javascript or JQuery [duplicate] - javascript

This question already has answers here:
Access Java / Servlet / JSP / JSTL / EL variables in JavaScript
(5 answers)
Closed 6 years ago.
First of all, I am not sure if this is even possible due to one being server side and other being client side.
Application that I am working on uses Spring MVC 3.1 and there is a Map (lets say validValuesMap ) of type Map<String,Long> defined in Spring method - referenceData.
Due to Map being defined in referenceData method, this Map is available in JSP page and I can use JSTL on this Map like - <core:if test="${(validValuesMap ne null)}"> etc.
Can I use this Map in plain JavaScript or jQuery too?
I have to basically perform a simple check on Long value of this Map if that is being greater than zero for a key and that key is available in the JavaScript function as a var.
I have to put that check into an else if (....) of JavaScript.
Is that possible? or any other way to perform that validation on client side for a button click event?

Convert that object to JSON on server side itself and then store it in javascript variable to manipulate it later on client side .

Related

store javascript data in PHP Variable [duplicate]

This question already has answers here:
How do I pass JavaScript variables to PHP?
(16 answers)
What is the difference between client-side and server-side programming?
(3 answers)
Closed 2 years ago.
I am not familiar with Javascript at all. I need to retrieve the value from externalReferralProgram . Which is in json format.
I want to get the value of this and store it in a PHP variable.
const FTXRest = require('./');
const ftx = new FTXRest({
key: 'apikey',
secret: 'apiseceret'
})
ftx.request({
method: 'GET',
path: '/api_key_status'
}).then(console.log);
PHP is a server-side language. Javascript is a client side language.
What happens is PHP will run whatever stuff its supposed to, then send whatever HTML and javascript you tell it to send to the browser. At this point, PHP will wipe out this session and start handling other requests. The browser then receives this content and starts running the javascript.
With this understanding, you can see that it really doesn't make sense to store a javascript value in a PHP variable - by the time the javascript is running, PHP has already long-forgotten about this request.
Your best bet is to either find a way to do the same javascript logic in PHP, or, make the javascript send a REST request back to the PHP server with whatever data you need, so that PHP can do further processing on it (this means creating a separate PHP file that'll receive javascript data from $_GET or $_POST).
Use json_decode
Takes a JSON encoded string and converts it into a PHP variable.

How to assign value in js variable to php variable [duplicate]

This question already has answers here:
How to use javascript variables in PHP [duplicate]
(4 answers)
Closed 4 years ago.
I made a javascript variable and assign some value
<script>
var score = 25;
</script>
after that i need that value inside php for any reason, unable to understand how to do so.
As JavaScript is a client-side language and PHP is a server-side language you would need to physically push the variable to the PHP script, by either including the variable on the page load of the PHP script (script.php?var=test), which really has nothing to do with JavaScript, or by passing the variable to the PHP via an AJAX/AHAH call each time the variable is changed.
If you did want to go down the second path, you'd be looking at XMLHttpRequest, or my preference, jQuerys Ajax calls: http://docs.jquery.com/Ajax

Put JavaScript variable value as the argument of PHP function [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 7 years ago.
I have this function in my PHP code.
<?php
$user = get_user_by_id($user_id);
?>
And I also have a JavaScript code on the same page.
The JavaScript generates some value to a JS variable.
What I need to do is, using that JavaScript variable value as the argument of the above PHP function.
So, what's the simplest method for achieving this..?
That's not quiet possible. You can't use client side variables (ECMA Script) inside your server side scripts (PHP). You may be able to achieve this by creating a web service and passing the variables using XHTTP Requests (AJAX) or reload the page using ECMA Script passing the value as a parameter and using it server side.
I don't know what exactly you are trying to do so I can't give you a definitive answer to this.

How do I persist query string throughout a user session without using a cookie? [duplicate]

This question already has answers here:
Change the URL in the browser without loading the new page using JavaScript
(14 answers)
Closed 3 years ago.
Is there a better way using javascript to persist url query string throughout user session in client side other than using cookie?
url:
http://site.com/?q=query
There is no other way to do that in HTML4.
Use window.name property. It'll store any string you need. And then check the property value on other pages.
Works on older browsers too.

Sending JSON Object from Javascript to Silverlight then triggering an event to update Silverlight

I am building a web application that basically consists of a set of HTML forms that, when the user updates them, updates a client-side Javascript Object Literal. That Object Literal is then converted to a JSON string and passed to the server for processing upon form submission.
User fill out form -> local javascript updates client-side JS Object Literal -> user presses submit -> JS Object Literal Converted to JSON object -> JSON Posted to Server -> server processes JSON object
Part of this application includes a visualization of the contents of the form. The visualization is very simple, consisting of a set of concentric circles. I am currently using HTML5 Canvas to create that visualization but would like to change the viz to utilize Silverlight as I would get better cross-browser results with hopefully less code.
My question is if it is possible to send a JSON string to a Silverlight application that is embedded in my client-side form and then trigger an event that updates the viz once that JSON object is received...all without a page reload.
Is this even possible with Silverlight and javascript? If so, a tutorial that covers this would be very helpful.
PS: I am using Visual Studio 2010 if that makes a difference.
TIA
Yes, you can do that - the HTML Bridge is a feature in Silverlight which allows JS code to call SL functions and vice-versa. Take a look at the link above for more information.

Categories