Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
Different ways to use local variables from HTML / Javascript in PHP

Recently I have been doing a lot of work in PHP and I have become familiar with how it works. I stand by what I have said before; That every problem has an endless amount of solutions. So that is what I am after, solutions that solve the same problem.

In this case, I want variables/references to values from localstorage:

localStorage.setItem("user", "bananaflakes55");
localStorage.getItem("user");

and directly include them in PHP files. Now I have found out that using echo have a variety of uses, for example:

echo '<script type="text/javascript"> window.location.replace("' . $refclinklogin . '"); </script>';

Granted that the value there are on serverside -> client side. In this case I want similar solutions that necessarily wont require me to create a GET or POST, with HTML elements like forms, that connect these.

To sum up, I want solutions that can bring values from local and session storage, to PHP. Bring forth some funky ideas, if possible. From what I have read it is a tricky one.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Even if i understand what you want, process sould be running from PHP to client rather than the reverse.

With this in mind, a light solution can be something like that :

window.addEventListener("load", function() {
    let request = new XMLHttpRequest();

    request.open("POST", 'localStorageToSession.php', true);

    request.onload = function () {
        let saveResponse = request.responseText;
        // if you want a callback or use some script return

    }
    let data = "jsonLocalStorage=" + JSON.stringify(window.localStorage);

    request.send(data);
}) ;

Once the page is loaded, send all localStorage parsed in json to a PHP treatment (here called localStorageToSession.php). So you can convert localstorage as $_SESSION. Something like that :

$_SESSION['jsLocalStorage'] = json_decode($_POST['jsonLocalStorage'], true) ;

Then you can use $_SESSION['jsLocalStorage'] in your backend treatments. Don't forget to add session_start() on all your files. You can save the xml request in a function and call once localStorage is updated).

Even if that solution works, i don't recommand it if you have to deal with safety informations like passwords or user special access.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!