简体   繁体   中英

Proper way to retrieve data from sessionStorage in php

I want to implement a code like this however i am getting an error as expected does anybody have any other way to do this or solve my error and make the code work. I have read that this can be done with AJAX but my teacher has forbidden me from using a language other than PHP javascript and html.

 <html>
 <body>
 <?php
 $user = "<script language='JavaScript'>var n=sessionStorage.getItem('user');document.write(n);</script>";

   //do something with $user
  ?>
  </body
  </html>

You can not retrieve sessionStorage or localStorage data (that exists on client) on server (PHP in you example). You have several options:

  • store data in cookies which can be retrieved on server side
  • send data via get http request (careful: URL limit size):
  • send data via post http request and hidden form field
  • and probably the best option, send data to server with AJAX.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM