简体   繁体   中英

How to put variable into cookie value

I need to put the email var in the cookie value, can I? This method don't work with "$email", in the \php\Newsletter-signup.php there is the $email var

     <?php
  include '\php\Newsletter-signup.php';

  echo '<script type="text/javascript">
                function createCookie(key, value) {
                    let cookie = escape(key) + "=" + escape(value) + ";";
                    document.cookie = cookie;
                    console.log(cookie);
                    console.log("Creating new cookie with key: " + key + " value: " + value);
                }
                createCookie("Email", "$email");
            </script>'
?>

You can do with this code (check howto interpolate a php variable inside a php string):

 echo '<script type="text/javascript">
     function createCookie(key, value) {
         let cookie = escape(key) + "=" + escape(value) + ";";
         document.cookie = cookie;
         console.log(cookie);
         console.log("Creating new cookie with key: " + key + " value: " + value);
     }
     createCookie("Email", "' . $email . '");
 </script>';

$email will not be expanded since you are using single quotes in your echo . Either use single quotes (and adjust any single quote inside your string) or use

createCookie("Email", "' . $email . '");

https://www.php.net/manual/en/language.types.string.php

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