简体   繁体   中英

Read Cookie Data from PHP in Javascript

I have a PHP file that contains both the PHP Code and the JQuery Code. My motive is to set cookies as and when the PHP code is executing and read the cookies set by PHP in the JQuery part of the code and make some decision. But I'm not able to do that. What would be the best way for me to achieve this.

I'm new to PHP any help would be great.

 <?php> if(isset($_POST['StartProcess'])){ $Process_step = 1; setcookie("MyCookie", $Process_step); sleep(30); $Process_step = 2; setcookie("MyCookie", $Process_step); sleep(30);} <?> <html> <head> <script type="text/javascript"> $(document).ready(function(){ window.setInterval(function(){ var abc = document.cookie.split(";").map(function(el){ return el.split("="); }).reduce(function(prev,cur){ prev[cur[0]] = cur[1];return prev },{}); console.log(abc["MyCookie"]); if(Step == 1){ do something;}else if(Step == 1){ do something else;} }) })</script> <head> <body></body> </html>

If I understand your question correctly, you would like to use PHP session value on the JQuery.

You can print the variable of PHP in the JQuery line. You can do it like this

var myvalue = " <?PHP $phpvarname ?> "

or you can use PHP session and assign it on the var.

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