0

Hello i want to save with cookie my color from input and if user refresh page color will be saved and ready to next use.Cookies must be set only for 1 hour and if expired cookies will be deleted.Sorry for my english and thanks for all help

<table>
<form action='' method='post'>
    <tr><td><label>URL adresa: </label></td><td><input type='text' placeholder='napr.google.com' name='url' /></td></tr>
    <tr><td><label>Titulek: </label></td><td><input type='text' placeholder='napr.google' name='title' /></td></tr>
    <tr><td><label>Vyberte barvu: </label></td><td><input type='color' name='color' /></td></tr>
    <tr><td><label>Otevřít v novém okně </label></td><td><input type='checkbox' name='window' />
    <tr><td></td><td><input type='submit' name='submit' /></td></tr>
</form>

<?php

if (isset($_POST['submit']))
{   
    if (!empty($_POST['url'])) 
    {
        if (!empty($_POST['title']))
        {
            $url = $_POST['url'];
            $title = $_POST['title'];
            $color = $_POST['color'];
            $explode1 = explode("/",$url);
            $explode2 = explode(".",$explode1[2]);
            setcookie("color", $color, time() + 3600, '/');
            if (isset($_POST['window'])) 
            {
                setcookie("window", $_POST['window'], time()+3600, '/');
                echo "<a style='color:" . $_COOKIE['color'] . "' href=" . $url . " target='_blank'>" . $explode2[1] . "." . $explode2[2] . "</a>";
            } elseif (isset($_COOKIE['window'])) {
                echo "<a style='color:" . $_COOKIE['color'] . "' href=" . $url . " target='_blank'>" . $explode2[1] . "." . $explode2[2] . "</a>";
            } else {
                echo "<a style='color:" . $_COOKIE['color'] . "' href=" . $url . ">" . $explode2[1] . "." . $explode2[2] . "</a>";
            }
        }
    }
}

?>

1 Answers1

0

You never save $color to your cookie:

if (isset($_POST['window'])) {
  setcookie("window", $color, time() + 3600, '/');
  echo "<a style='color:" . $color . "' href=" . $url . " target='_blank'>" . $explode2[1] . "." . $explode2[2] . "</a>";
}

Or if $color needs to be saved separately:

setcookie("color", $color, time() + 3600, '/');

Personally, I would save it to a Session like so:

session_start();
$_SESSION['color'] = $color;

Hope that helps. Edit:

if (isset($_POST['submit']) && (!empty($_POST['url']) && (!empty($_POST['title'])) {
  $url = $_POST['url'];
  $title = $_POST['title'];
  $color = (($_POST['color'] == $_COOKIE['color'])?$_COOKIE['color']:$_POST['color']);
  $explode1 = explode("/",$url);
  $explode2 = explode(".",$explode1[2]);
  setcookie("color", $color, time() + 3600, '/');
  if (isset($_POST['window'])) {
    setcookie("window", $_POST['window'], time()+3600, '/');
    echo "<a style='color: $color' href='$url' target='_blank'>$explode2[1]}.{$explode2[2]}</a>";
  } else {
    echo "<a style='color: $color' href='$url'>{$explode2[1]}.{$explode2[2]}</a>";
  }
}
Twisty
  • 30,304
  • 2
  • 26
  • 45
  • oh ... i mean window its my bad... but color i need too – Jakub Stanek Apr 09 '15 at 20:19
  • try `setcookie("window", $_POST['window'], time()+3600, '/');` – Twisty Apr 09 '15 at 20:20
  • i will try it but i want to ask you can i use this set cookie at the beginning or if (isset($color)), but this input color is always set, because default color is black, so i dont know – Jakub Stanek Apr 09 '15 at 20:23
  • Depends. If you are concerned that $color might not be set or is empty, then you would want to check before hand. `$color = isset($_POST['color'])?$_POST['color']:""; if(!empty($color)){ setcookie("cookie", $cookie, time()+3600, '/'); }` – Twisty Apr 09 '15 at 20:33
  • but there is one problem color is always set and never is empty, becasue as i said color is set as black – Jakub Stanek Apr 09 '15 at 20:35
  • Then don't check it. Just dump it right into the cookie. – Twisty Apr 09 '15 at 20:36
  • but i dont understand if i set cookie all is okey cookie is set but color is still black.. – Jakub Stanek Apr 09 '15 at 20:37
  • Can you update your original post with the code you are now using? – Twisty Apr 09 '15 at 20:39
  • Window is okey now it works, but color is not work and i dont understand it – Jakub Stanek Apr 09 '15 at 20:44
  • It's shorthand for an `if` statement: http://stackoverflow.com/questions/23752980/php-meaning-of-question-mark-colon-operator (statement)?{true result}:{false result}; – Twisty Apr 09 '15 at 21:23
  • yes.. i dont understand this short form if statement is better for me to understand it – Jakub Stanek Apr 10 '15 at 04:50
  • Want to help you out, yet you have not provided new code examples or errors. What is your test, what are the expected results? – Twisty Apr 10 '15 at 19:13
  • 1
    Cookies are now okey i did it only with setcookie in code and put value='$_COOKIE['color']' in input type color that was all. Thanks for help – Jakub Stanek Apr 11 '15 at 21:06