将 jscolor 从一个 php 页面传递到另一个页面

Pass jscolor from one php page to another

我正在使用 jscolor 并尝试将用户选择的颜色从 test1.php 传递到 test2.php,然后转换为 rgb。不知何故不起作用。

如果我将 post 的形式更改为 test1.php,它会检索到正确的值,但是当更改 action="test2.php" 时,则无法正常工作。

test1.php:

<script>
function update(jscolor) {
    // 'jscolor' instance can be used as a string
    document.getElementById('rect').style.backgroundColor = '#' + jscolor
}
</script>

 <form action="test2.php" method="post">
     Select you favorite color:
     <input name="clr1" class="jscolor {onFineChange:'update(this)'}">
     <input type="submit" name="submit">
 </form>
 <p id="rect" style="border:1px solid gray; width:161px; height:100px;">

<?php 
  session_start();

  if(isset($_POST['clr1'])) { 
      $selected_color = $_POST['clr1']; 
      $_SESSION['bgcolors'] = $selected_color;
  }
?>

test2.php:

session_start();
$selected_color = $_SESSION['bgcolors'];
echo $selected_color;
echo "<br>";
list($r, $g, $b) = array_map('hexdec', str_split($selected_color, 2));

$bgcolor2 = $r . "," . $g . "," .$b;
echo $bgcolor2;

session_start这段代码后test2.php添加

 if (isset($_POST['clr1'])) { 
    $selected_color = $_POST['clr1']; 
    $_SESSION['bgcolors'] = $selected_color;
 }

在 php 第 2 页中,您将要通过使用 session_start 启动会话来检索变量,之后您可以使用 $_SESSION['bgcolors']

但是我可以看出您的代码存在问题,因为您没有首先想到调用 session_start。

Always call session_start unconditionally. Always call session_start before you output anything on the page.

如堆栈响应所示 What is proper way to calling session_start()