How to save an option in a dropdown menu as a php session variable?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rose merry
    New Member
    • May 2015
    • 14

    #1

    How to save an option in a dropdown menu as a php session variable?

    I am trying to save one of the options in a dropdown menu as a session variable.
    Code:
    <select name="number" id="number">
                <option>one</option>
                <option>two</option>
                <option>three</option>
            </select>
    
                <?php
             session_start();
    $_SESSION['number'] = $_POST['number'];
    ?>
    What it returns in the browser page is:

    Notice: Undefined index: number in C:\xampp-portable\htdocs \eva\00ideasele ction.php on line 46
    What is the error here?
    What is the best way to save an option from a dropdown menu as session variable?
  • computerfox
    Contributor
    • Mar 2010
    • 276

    #2
    I would suggest building a method in the header of the page that takes in a parameter of the value you want to set, then set the cookie or session. That is to say:

    Code:
    <html>
      <head>
        ....
        ....
        <?php 
         function set_session($value){
          setcookie("Option",$value);
         }
        ?>
      </head>
      <body>
        /// put form data here
    
       <?php
         if(islet($_POST['submit'])){
          set_session($_POST['number']);
         }
        ?>
       </body>
    </html>
    I am also posting a link to a great resource I recommend you look over:
    We're a group of established PHP projects whose goal is to talk about commonalities between our projects and find ways we can work better together.


    Hope that helps!

    Comment

    • rose merry
      New Member
      • May 2015
      • 14

      #3
      thank you so much.....

      Comment

      Working...