Get localStorage value into php [duplicate] - javascript

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.

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.

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!

Problems with javascript inside php [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
Here I need in some way to concatenate my js variable num with php query if it's possible?
Because, I have two select tags (in my html page) the second one depends from the first one. On blur I can get the value of the first, but I don't know how to concatenate this value to the php query which needs to load the second select.
<?php
echo "<script>";
echo "var num = 5;";
$query2="SELECT * FROM region WHERE IDNation=";
echo "</script>";
$res2 = mysqli_query($con,$query2);
?>
Basically php is server side language. You can assign any value to php variable during page loading.
Javascript works on the user's computer, PHP works on your server. In order to send variables from JS to PHP and vice versa, you need communication such as a page load.
2nd option is you can use cookies
1) First add a cookie jquery plugin.
2) Then store that window width in a cookie variable.
3) Access your cookie in PHP like $_COOKIE['variable name'].(http://www.w3schools.com/php/php_cookies.asp)

WebSQL Database Upload [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
WebSQL Database Upload
I have a WebSQL database, and am trying to connect to a PHP script, and dump that database into another one on the server.
How do I format the javascript / jquery in order to do that correctly?
I guess I am asking, how do I dump all of my data in my tables into my PHP script, so that it can update and insert the data into another database on the server?
put all the localStorage or whatever local database you have into a JSON object using js/jQuery/etc., then POST that JSON object, and then using php, go through that POST'ed JSON object and use php code to dump it into your database.

passing javascript variable value to JSP variable [duplicate]

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.

Categories

Resources