passing javascript variable value to JSP variable [duplicate] - javascript

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
passing value to JSP via javaScript
i want to pass value of a javascript varible to jsp variable is this possible?

This will require two things:
Your Javascript to somehow post the variable's value, either normally (e.g. in a HTTP POST field) or with AJAX to the web server.
The controller needs to fish it out of the request (there are numerous ways of doing that) and supply it to the JSP.

Well, you can't. Javascript is executed on the browser & what comes out there is only HTML. So you can use javascript to set form fields or send them as request variables using AJAX.
You can use your jsp scriplets inside of java script which gets parsed on your server & the values can then be used. for ex:
var i = <%=initialValue%>
When the jsp is processed on the server the scriplets are going to be parsed. What comes out to your browser will be
var i = 10; // In case your initialValue was 10 in your jsp.

Related

How can I use PHP $_SESSION in javascript function? [duplicate]

This question already has answers here:
Why PHP script is not workig in a web browser?
(9 answers)
What is the difference between client-side and server-side programming?
(3 answers)
Closed 4 years ago.
Okay, here are something I have now.
I'd like to use $_SESSION['pass'] in my other php files to make it as a condition to check whether an if condition is true. But it seems that the system only shows the alert part and never runs the code in the PHP tag.
Thanks a lot!
document.getElementById('pass').onclick = function(){
<?php $_SESSION['pass']=1; ?>
alert('here!');
}
document.getElementById('fail').onclick = function(){
<?php $_SESSION['pass']=0; ?>
alert('there!');
}
PHP is a server-side scripting language whereas JavaScript is a client-side scripting language. PHP runs in the back end on the server whereas JavaScript runs in the front end on the browser. PHP can be used to generate dynamic content for the browser. In your case, it seems like you want to set value of a session variable based on condition in the JavaScript code. PHP blocks in your code are executing in first place on the server which runs all PHP blocks by first setting $_SESSION['pass'] as 1 and then to 0 in the next PHP block. The resulting HTML is then sent to the browser for display. If you want to change the value of session variable based on the button user clicks, then you will need to do it using AJAX request.

Get localStorage value into php [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Passing HTML5 Local Storage Value to PHP error [duplicate]
(4 answers)
Closed 5 years ago.
I have a variable set in localStorage and I want to get it into php. When php is executed the value is already set in local storage, but how should I get into php?
I tried something like this:
$myvar = "<script> localStorage.getItem('productinfo')['s3_url']</script>";
But this for some reasons gives a Uncaught SyntaxError: missing ) after argument list
How should I approach this? Thanks.
You can not directly get a localStorage value in PHP, when script is executed, because localStorage is contained in user's browser, and PHP is running on server. However, you can send the value from localStorage to a server with GET or POST request.
You can't get local storage values from the server. You should use cookies instead. Local storage is client-side only. Cookies are for both. You can get the value with Javascript and then send it to the server in a form or with ajax, or set a cookie with the value and use $_COOKIE to access it.

what is the difference with "test" javascript variable and "#test" variable can i convert var javascript to "#test"

I've seen a servlet between the JSP and the database that is implemented similar this page. the important part for me is this:
param p1fd = new param();
p1fd.setVal(request.getParameter("formDDL"));
where the parameter in jsp code is referenced as "#formDDL"
the problem is that I have formDDL as a javascript variable as such:
var formDDL;
How can I convert the javascript variable to #formDDL?
Is there any other way to I pass this variable to Expression Language written for inserting in mySQL code?
The scriptlet part of the JSP is executed on the server side prior to it being returned to the client. Therefore you can't change its value after the page is sent to the client. You would have to have a preliminary page that sets the value of formDDL in javascript and then call into the server to request the page with the formDDL included as a request parameter or POST data. Basically the same approach that was done in the page you linked.
If you didn't want to have to fully reload the page to change the value of formDDL then you would have to take a completely different approach and look into using Ajax.
Also be mindful of SQL injection attacks when following a similar approach to the one you linked to.

How to save a JS variable value on PHP site? [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
I'm writing a little online programm that must save a football tournament results. I'm adding a team names using JS, and i want to save them on server site (in text file). For this i need PHP, i think. JS looks like this:
<script>
function addTeam(){
var name = $("#FormForNameEntering").val();
if (name != ""){
$("#TableDataNameContainer").append("<p class='teamName'>" + name + "</p>");
$('#FormForNameEntering').val("");
}
}
</script>
So i need to save variable "name" in text file. For this i need to tranfer its value to PHP? How?
You have three options. You would make an Ajax call, form submission of some kind with a submit button and action to be set to a php file, or finally have your whole page in a php file and after receiving data in JavaScript do your php there.
You need to send it to the server! e.g:
window.location = www.example.com?variableName='+yourJSVariable
And just retrieve it via PHP's $_GET. Other option you have is sending it to the server asynchronously (=in background lets say). Have a look at jQuery's ajax/get/post functions, or play around with XHR obj in js. Or try submiting via html forms.
Dont worry we've all started somewhere!

Transferring value of javascript variable to asp variable

I've designed a javascript function which returns out an integer value (and before anyone says, no this cannot be done with asp or any server side scripting, at least to my knowledge). And I need the contents of the variable containing the integer value produced to be transferred into an asp variable so that I may do some server side scripting with it. Does anyone have any idea as to how to do this? I'm tearing my hair out over this!
Well, the preferred method to change server side status (variables) in the browser without reloading the whole page would be to use AJAX.
It's not necessary, but using jQuery, you could just call get:
// we are in javascript
var my_value = 42;
$.get("set_my_value.asp?to=" + my_value);
By this, you can change some data on the server side (e.g. as Session variable or in a database):
' classic ASP page (set_my_value.asp)
dim my_value
my_value = Request.Querystring("to")
Session("MyValue") = my_value
I don't think there's a way to do it directly. You can populate a hidden input in a form with the javascript value. Then on posting the form you can use the value using request("MyHiddenField")
You could try to do this via Cookies.
Your javascript function will store value in some Cookie.
In ASP code you reading Cookie.
This cookie could be specific for each user/session, just do not forget to send Cookie name from ASP to JavaScript function.

Categories

Resources