简体   繁体   English

使用Javascript将PHP转换为HTML 5

[英]PHP to HTML 5 using Javascript

I have a difficult problem, we are currently using PHP on our server and I am a PHP programmer. 我遇到了一个难题,我们目前在服务器上使用PHP,并且我是一名PHP程序员。 I have built a site connected to our PHP site that is completely client side HTML 5 and Javascript. 我已经建立了一个连接到我们PHP网站的站点,该站点完全是客户端HTML 5和Javascript。 I can pass variables from HTML 5 to PHP but I want to import some of the data from the PHP to the HTML pages such as session ...etc. 我可以将变量从HTML 5传递到PHP,但是我想将一些数据从PHP导入到HTML页面,例如session ... etc。 how can I pass PHP sessions to Javascript on another page or access the session data to be used on HTML 5 and Javascript? 如何将PHP会话传递到另一页上的Javascript或访问要在HTML 5和Javascript上使用的会话数据?

The main reason for this is for users with low bandwidth and for the ability of HTML 5 to use local storage and offline capabilities if their line is disconnected so they can post the data at a later time to the PHP pages. 这样做的主要原因是带宽较低的用户以及HTML 5在断开线路连接的情况下使用本地存储和脱机功能的能力,以便他们可以稍后将数据发布到PHP页面。

Help would be great? 帮助会很棒吗?

I hope I am not comepletely wrong here, but what you are essentially trying to do is writing data from php to javascript. 我希望我在这里不会犯错,但是您实际上要尝试做的是将数据从php写入javascript。 You could use AJAX as akluth suggested, but I think it is easier to just do the following in the javascript; 您可以按照建议的方式使用AJAX,但我认为在javascript中执行以下操作会更容易;

data = <?php echo $foo; ?>;

You should however be careful when saving the session in localstorage since it can be modified by the client. 但是,将会话保存在本地存储中时应小心,因为客户端可以修改它。 Probably double check once the user attempts to post the data later. 一旦用户尝试稍后发布数据,可能会仔细检查。 See this SO answer for more details on that. 有关更多详细信息,请参见此SO答案

If you want to get session data in PHP you can use following segment 如果要在PHP中获取会话数据,则可以使用以下段

<script>var $_SESSION = <?= json_encode($_SERVER) ?></script>

If some data to be maintained on different pages and not require sever data, better you use Session Storage of HTML5 [http://www.xul.fr/en/html5/sessionstorage.php] 如果某些数据需要保存在不同的页面上并且不需要服务器数据,那么最好使用HTML5的会话存储[http://www.xul.fr/en/html5/sessionstorage.php]

This will reduce server load + data transfer and increase speed of your application 这将减少服务器负载+数据传输并提高应用程序速度

HTML5 local storage is not fully supported so some thing that can work anywhere can be do in following way: First have your PHP Session into JS var: 不完全支持HTML5本地存储,因此可以通过以下方式完成某些可以在任何地方使用的操作:首先,将您的PHP Session放入JS var:

 <script type="text/javascript">
         var sessionData= <?php echo json_encode($_SESSION); ?>
 </script>

Then you will have session data in sessionData variable in JSON format. 然后,您将在sessionData变量中以JSON格式获取会话数据。 then if you want to use it on different places then simply set and fetch it into cookies via Javascript. 然后,如果您想在其他地方使用它,则只需将其设置并通过Javascript将其提取到Cookie中。

Please tell if some thing is still unclear. 请告诉我们是否还有不清楚的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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