简体   繁体   English

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

[英]Pass jscolor from one php page to another

I'm using jscolor and trying to pass the users selected color from test1.php to test2.php and then convert to rgb. 我正在使用jscolor,并尝试将用户选择的颜色从test1.phptest2.php ,然后转换为rgb。 Somehow is not working. 不知何故不起作用。

If I change the form post to test1.php it retrieves the correct value, but when changing action="test2.php" then is not working. 如果我将表单发布更改为test1.php它将获取正确的值,但是当更改action="test2.php"时, action="test2.php"无法正常工作。

test1.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: 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;

Add in test2.php after session_start this code session_start之后添加test2.php

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

In php page 2 you are going to want to retrieve the variable by starting the session with session_start and after that you can access it with $_SESSION['bgcolors'] 在php页面2中,您将要通过使用session_start启动会话来检索变量,然后可以使用$ _SESSION ['bgcolors']访问该变量

However I can see a problem with your code in that you are not calling session_start the first think you do. 但是,我可以看到您的代码存在问题,因为您没有以您想的那样调用session_start。

Always call session_start unconditionally. 始终无条件调用session_start。 Always call session_start before you output anything on the page. 在页面上输出任何内容之前,请始终调用session_start。

As seen in this stack response What is proper way to calling session_start() 如该堆栈响应所示,调用session_start()的正确方法是什么

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM