-3

我想存储一个会话变量并回显它。我不知道该怎么做。目前我有以下代码:

<script>
  function changecolor(obj)
  {
    obj.style.backgroundColor="yellow";
  }
</script>


<table id="table">
  <tr>
    <td id="glow1" bgcolor="green" onclick="changecolor(this)" >8-9  /td><br>
    <td id="glow2" bgcolor="green" onclick="changecolor(this)" >9-10 /td><br>
    <td id="glow3" bgcolor="green" onclick="changecolor(this)" >10-11 /td><br>
  </tr>
</table>

<form action="sample.php" method="post">
  <input type="submit" name="submit" value="confirm">
</form>
4

1 回答 1

-1

JS:

function changeColor(color) {
            $.ajax({
                type: "POST",
                data: { 
                        'color': color
                    },
                url: '/url/to/script.php',
                cache: false,
                success: function(data){
                    $('#table td').css('background-color',data);
                }
            });

}

索引.php:

<?php
   session_start();
   if (isset($_SESSION['color'])) {
        $color = $_SESSION['color'];
   }else{
        $color = '#FFFFFF';
   }
?>
<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    </head>
    <body>
        <table id="table" border="1">
            <tr>
                <td onclick="changeColor('#FF0000');" bgcolor="<?php echo $color;?>">Turn red</td>
            </tr>
            <tr>
                <td onclick="changeColor('#00FF00');" bgcolor="<?php echo $color;?>">Turn green</td>
            </tr>
            <tr>
                <td onclick="changeColor('#0000FF');" bgcolor="<?php echo $color;?>">Turn blue</td>
            </tr>
        </table>
     </body>
</html>

脚本.php

<?php
    session_start();
    $_SESSION['color'] = $_POST['color'];
    echo $_POST['color'];
?>
于 2013-07-26T07:03:46.043 回答