1

I am trying to save one of the options in a dropdown menu as a session variable.

<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\00ideaselection.php on line 46

  1. What is the error here?
  2. What is the best way to save an option from a dropdown menu as session variable?
2
  • Seems like your $_POST doesn't have the number index. Recheck that. There isn't enough code here to debug it. Commented May 21, 2013 at 12:15
  • You are trying to access a POST parameter before even submitting the form and thereby sending any POST data … look up isset/empty in the PHP manual. Commented May 21, 2013 at 12:19

3 Answers 3

2

Add value to the drop down

<select name="number" id="number">
      <option value="1">one</option>
      <option value="2">two</option>
      <option value="3">three</option>
</select>

and while retriving the value use isset function to check whether the value has been posted

<?php
    session_start();
    if(isset($_POST['number'])){
        $_SESSION['number'] = $_POST['number'];
    }
 ?>
Sign up to request clarification or add additional context in comments.

1 Comment

It worked both with value or not. If I don't put the value attribute it will save the text at the session.
0

Try this

<select name="number" id="number">
        <option>one</option>
        <option>two</option>
        <option>three</option>
    </select>

   <?php
     session_start();
     if(isset($_POST['number']))
         $_SESSION['number'] = $_POST['number'];
  ?>

The error you see is because you have not posted the form and you are trying to get the value from $_POST. The $_POST and $_GET has data from posted form. So when you are posting your form to same php script where the form is created, then when the form is loaded without form post, it will show you that notice message. To avoid notice message, i place an

   isset($_POST['number'])

check, this way no notice will be shown.

When the form is posted, and you get $_POST, then the value will be set into session.

Thanks

1 Comment

It worked. The reason, it did not work, was that I had put both php and html patches in the same file. I created another one and via a form I called the php file.
0
<select name="number" id="number">
      <option value="1">one</option>
      <option value="2">two</option>
      <option value="3">three</option>
</select>

while retriving the value use empty() function to check whether the value has been posted and is not empty. isset() not checks as numbers are passed as an argument in $_POST as string

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.