How can I pass a JavaScript variable to a Java program? - javascript

Essentially, the question I am asking is similar to this question. However, these Q/A's were of little value to me.
Firstly, there are several people saying that it is impossible to pass a value from a clientside language to a serverside language. However, I know that this is untrue, since I know that it is possible to pass javascritpt (clientside) variables to php (serverside) variables.
Secondly, my need to do this is based on trying to retrieve data from a 3rd party website, to be processed by my program, which will not attempt to return this data to the origional web page. Instead it will be returned to a different location. So, suppose I am retrieving the data required from their webpage using javascript and storing it within a Javascript variable, say var myData = *my required data* and I wish to create a Java program which accepts this data as an input, how would this be done?

You can always send client side information to the server. This works in Java as well.
You can make an AJAX request and pass your variable with it.
$.ajax({
type: 'post',
url: 'myserverurl.ajx',
data: 'data=' + variable1 + '&data2=' + variable2
});
You can do this without having to make an async call as well.

Related

How to pass value of variable in procedure to Javascript function in Apex

I have variable in local procedure that rendering data to pop-up window
I'm using oracle apex 20.2
Oracle Database 18 c
Variable name :l_factor_count
I want to pass this variable value to be used in Javascript function inside this procedure
After I did some research I found that this variable is local and needs to be related with page item but I don't know how do that because I use the procedure to do all the work and rendering data in pop-up as I said before.
There are 2 ways of doing what you want
Divide you code in 2 parts.
1º Execute the PL/SQL required sending the values e returning them accordingly. Submitting this values in a dynamic action is APEX 101, so you should be fine here.
2º Execute a second action with the JavaScript code grabbing these values.
Execute an Ajax Call
Use apex.server.process to call you PL/SQL function and make it return your values to continue the process.
I do not advise you to my JavaScript and PL and if possible try to avoid it.
If you don't know how to do the steps from either item, then you might be doing stuff that, at the moment, is to advanced for you. So please check out some tutorials, anything online is going to help a lot.

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 can I display and update PHP objects in HTML?

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.

avoid mismatch between server side and client side result

I use PHP and Javascript. In my website some results are processed server side some client side.
Using javascript only, prevents your website from being crawled correctly by search engines and using PHP only prevents correct real time response.
The problem is how to grantee both js functions and PHP functions give the same result? for example suppose there is a function which gives relative time:
JS:
function relative_time(timestamp)
{
...
}
PHP:
function relative_time($timestamp)
{
...
}
Keeping both functions matched with each other is not easy since I want to edit both. For example if both give us:
one year ago
And I change PHP only, to give me:
a year ago
Then JS is not updated too. Is there any standard way to ensure both will act in the same way?
unfortunately js function cannot be called on server side.
If there is some complicated logic, you should implement it server-side and just pull the results via AJAX. That way you only need to maintain the PHP code and provide a kind of AJAX API for access via JS.
I think you just need to make a decision where it is to be done, because if they do vary which is to be dominant ? (that's the one that should be doing it)
Also, how are you saving server load by doing it in both locations ?
Avoid this by making a decision for which code is to do it, failing that, Put a note in you code at both locations reminding yourself to update both locations ?

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.

Categories