0

I can't put whole page's code so just I'm putting only about session variable code. So code divide in to 3 page

Error is I'm getting in displaye.php i.e Undefined index: size

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
  }
?>
Shiladitya
  • 12,003
  • 15
  • 25
  • 38
suraj joshi
  • 31
  • 1
  • 7

2 Answers2

1

Your question is not very clear, something closest to how I understand it is as follows:

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>
mountain
  • 106
  • 1
  • 14
0

There is not opening tag here so I'm assuming that you didn't put it or that you forgot to put it on your question. Also, you are trying to submit your form with an anchor tag.

The form tag implies the GET method as default, but in savesize.php you are getting a POST variable.

So you should

  1. add the correct form tag in 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. Check on savesize.php if the POST value has been submitted:

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

Then you should see your size in displaysize.php

J0ker98
  • 447
  • 5
  • 18