How to create image elements with php and the source is a cookie i have set earlier? I got some code, but it seems not working... Nothing is happening when i refresh the page.

I got a javascript code that set the first cookie to link1 with a value that is wrote in the top input box, and next cookie sets to link2 and so on.

My goal is to output 4 image elements if i have 4 "link cookies" set.

Here's what i got so far.

<form>
    <input id="link" type="text" placeholder="länk address">
    <input id="image" type="text" placeholder="bild address">
    <button onClick="createLink();">Lägg till länk</button>
</form>
 <?php
    for ($x = 1; isset($_COOKIE["link.$x."]); $x++) {
         $_COOKIE["link.$x."] = $link;
         $_COOKIE["image.$x."] = $image;
         echo '<a href="'.$link.'"><img src="'.$image.'"></a>';
    }
?>
有帮助吗?

解决方案

Your assignments are backwards. You want to read the cookie, not set the cookie. Also, you don't use . if you're interpolating a variable inside a string, only when you're concatenating strings.

for($x=1; isset($_COOKIE["link$x"]); $x++){
     $link = $_COOKIE["link$x"];
     $image = $_COOKIE["image$x"];
     echo '<a href="'.$link.'"><img src="'.$image.'"></a>';
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top