Accesing JavaScript variable in JSP [duplicate] - javascript

This question already has answers here:
How can i access javascript variables in JSP?
(3 answers)
how to access the javascript variable in jsp [duplicate]
(3 answers)
Closed 5 years ago.
I have a JavaScript variable and I want to access it in JSP without using submit button. Actually I have a javascript variable and i want to use it for running a query in JSP to fetch a value from database and again using the value from JSP to javascript.
I do not want to have a button in the form tag for submitting the value and then getting it as a paramneter in JSP. Is there any other way of doing that.
Actually i have atextbox where username is coming. I want to use that variable so that i can run a query in JSP and find its user Id. Code for getting value of text box is.
var selUserInfo = thisFrm.SelUser.value
Now i want to pass this selUserInfo to a JSP.

Not possible unless you send a GET or POST request. See this explanation.
Edit: after understanding that you're aiming for an autocompletion text-input, I would recommend to look for a jQuery solution like this one, and using an Ajax request to retrieve the data from your database (inside a .ready() event).

Related

I want to access a javascript variable in my HTML in my main node js file [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 2 years ago.
function takePic(){
var dataURL = canvas.toDataURL();
}
So i have this function in a script tag nested in my HTML. I have a separate js file to start the server. My problem is I want to use this variable/data in my js file but I don't know how to access it.
Hmmm sounds like you need to send the variable data across the internet from your html webpage to your node.js server. To do that you could look into using socket.io to send data back and forth.

How to use javascript variable in php code in javascript function itself? [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 4 years ago.
I want to get php array element using javascript variable as an index of a php array. I have tried this below method but it doesn't work.
// I want to get value of $arr[0] and here array index is a javascript variable
<script type="text/javascript">
function select_seller()
{
d = document.getElementById('seller_id').value;
document.write("<?php echo $arr['<script>document.write(d)</script>'];?>");
}
</script>
Please Help me
You can't do this. PHP code is executed first so once the user loads the page, there's no way for PHP to detect a JavaScript variable.
If you posted some more code we could probably tell you how to do what you are trying to do in JavaScript.
You don't seem to know the difference between server-side and client-side, your php code is interpreted on the server to produce html data and send it to the browser to render it to the user, hence you cannot use php function on the client-side .. php role is preparing and sending data to the client-side.
so whatever you are trying to achieve here you have the option of using ajax calls to get data from the server and process it with js upon receiving it.

Why does jQuery.data() not update the element [duplicate]

This question already has answers here:
jQuery Data vs Attr?
(3 answers)
Closed 6 years ago.
Im having an issue or possibly a expected result with JQuery's .data() method. Im unsure why when using the element selector the data values are not updated after running the data method. See the screenshot for an example of what im talking about.
Im unsure why when using the data(key,value) method, it updates the elements data with the expected output. However why is it that it doesnt updated the values html attribute? My knowledge in Javascript is still very limited, but i'd love to know what this is and if i should expect this.
jQuery reads all data-attributes on pageload. It then deals with changes by the .data()-function only in it's storage.
To update the "real" HTML-Attribute, you need to manipulate the content like so:
$('.selected a span:eq(3)').attr('data-value', 'newValue');

How to get a variable from php to JS and back [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
First of all, I'm relatively new to this so I'm sorry if I word stuff weird.
So let's say I have the variable $x with the value 1 in PHP. I want to get the variable to my JS-Script and modify it and then send the variable x with the modified value back to my PHP document. Note I don't want to send it to a server and I don't want to use AJAX (have yet to learn it). If there's no other way It'd be nice if someone could show me an example of how to do this with AJAX.
You should use AJAX if possible it is more comfortable and with jQuery not hard to use/understand. But if this topic is new for you, you can try to send it via a window.location.href.
I looked for some good examples for you:
PHP to JS
Without AJAX
Video - JS to PHP with jQuery POST
To save PHP variables from PHP to Javascript you often just print them. If you're more familar with Javascript and PHP, you should try to work with JSON-Strings. But I think that would be to much for your first try.
Hope that helps.

Javascript/JSP: How to pass JSP variables value on one page to javascript variables on next page? [duplicate]

This question already has answers here:
Access Java / Servlet / JSP / JSTL / EL variables in JavaScript
(5 answers)
Closed 7 years ago.
Currently I am trying to send JSP value to javascript. Is it possible? My cases is as follows:
On Page abc.jsp, user enters data in form. Which is send on the next page xyz.jsp. I want to use this data in xyz.jsp pages javascript. Is it possible? Is yes how to do this?
also would like to know, if I auto refresh this page(or part of page) ie xyz.jsp then is it possible without javascript failing or crashing?
Thanks.
Just let JSP print it as if it is a JS variable. Assuming that you've variable ${foo} in JSP:
<script>var foo = '${foo}';</script>
This will end up in webbrowser as
<script>var foo = 'somevalue';</script>
Keep in mind: JSP runs at webserver and produces HTML. JS is part of HTML and runs at webbrowser.
See also:
How to communicate between JavaScript and Java/JSP/JSF?

Categories

Resources