如何将选定的下拉值存储到会话变量中并显示在另一页上

时间:2018-11-10 10:49:42

标签: php session session-cookies

我不能放入整个页面的代码,所以我只讲会话变量代码。因此,代码分为3页

错误是我进入displaye.php,即未定义索引:大小

select-size.php

<?php session_start();
  include 'connection.php';?>

   <label for="size">size</label>
     <select class="form-control" id="size" name="size">
        <option>Chooze size</option>
        <option value="s" ><?php echo $row['s_size']; ?></option>
         <option value="m" ><?php echo $row['m_size']; ?></option>
       <option value="l" ><?php echo $row['l_size']; ?></option>
       <option value="xl" ><?php echo $row['xl_size']; ?></option>
       <option value="xxl" ><?php echo $row['xxl_size']; ?></option>
     </select>

  <a href="save-size?id=<?php echo $row['id']; ?>"> <button class="btn btn-primary" ><i class="icon-bag"></i> Add to Cart</button> </a>
  1. savesize.php

    <?php
      session_start();
        $_SESSION['size'] = $_POST['size'];
     // $_SESSION['size'] = "some masssage "; -- if i wrote msg instead of passing selected value its shows on output page 
    ?>
    

3.displaysize.php

<?php
 session_start();
 //info message
   if(isset($_SESSION['size'])){ ?>
   <h2> size: <?php echo $_SESSION['size']; ?> </h2>
 <?php
  }
?>

2 个答案:

答案 0 :(得分:1)

您的问题不是很清楚,最接近我的理解的是:

select-size.php

        <form  method="post" id="form1" name="form1" action="savesize.php">
           <label for="size">size</label>
             <select class="form-control" id="size" name="size">
                <option>Chooze size</option>
                <option value="s" >s</option>
                 <option value="m" >m</option>
               <option value="l" >l</option>
               <option value="xl" >xl</option>
               <option value="xxl" >xxl</option>
             </select>
             <input type="submit"  value="Submit Size"  class="submit" >
        </form>

savesize.php

        session_start();
        if (isset($_POST['size'])) {
            $size = $_POST['size'];
            }
        $_SESSION["SizeChosen"] = $size;  
        header("location: displaysize.php?size=$size");

displaysize.php

        session_start();
        $size=$_GET['size'];
            ?>
        <table border="1">
        <tr>
        <td>Size Chosen:</td><td><?php echo $size ?></td>
        </tr>
        <tr>
        <td>Using Session As Chosen:</td><td><?php echo $_SESSION["SizeChosen"] ?></td>
        </tr>
        </table>

答案 1 :(得分:0)

这里没有开场标记,因此我假设您没有将其放进去或忘记了将其放在您的问题上。另外,您正在尝试使用锚标记提交表单。

form标记表示默认为GET方法,但是在savesize.php中,您将获得POST变量。

所以你应该

  1. 在select-size.php中添加正确的表单标签:

    <?php 
     session_start();
     include 'connection.php';
    ?>
    
     <!-- We are adding here your ID as a GET parameter while sending the other form data as POST -->
     <form action='savesize.php?id=<?php echo $row['id']; ?>' method='POST'> 
         <label for="size">size</label>
         <select class="form-control" id="size" name="size">
            <option>Chooze size</option>
            <option value="s" ><?php echo $row['s_size']; ?></option>
            <option value="m" ><?php echo $row['m_size']; ?></option>
            <option value="l" ><?php echo $row['l_size']; ?></option>
            <option value="xl" ><?php echo $row['xl_size']; ?></option>
            <option value="xxl" ><?php echo $row['xxl_size']; ?></option>
         </select>
    
         <button class="btn btn-primary" type='submit'><i class="icon-bag"></i> Add to Cart</button>
     </form>
    
  2. 检查savesize.php是否已提交POST值:

    <?php
      session_start();
      if(!empty($_POST['size'])) {
         $_SESSION['size'] = $_POST['size'];
      }
    ?>
    

然后您应该在displaysize.php中看到尺寸