Ohad
Ohad

Reputation: 1631

Why is my <select> element's value is not present on the server?

Would it be legal to do something like this in order to get the user's selection from the drop-down list?

I know that I can use the $_REQUEST['name'] when it's <input type="text"> but I am not sure if the request array saves the <select> value as well as I am not getting the correct output.

The output should be: the name of the selected item. For example if I selected "drink coffee" It should print "drink coffee".

<?php
  $toDo=$_REQUEST['do today'];
  echo $toDo;
?>

<!DOCTYPE html>

<html>
  <body>

    <form action="planner.php" method="post">
       what would you like to do today?
       <select name="do today">
          <option value="drink coffee" >drink coffee</option>
          <option value="read book"  selected="selected">READ BOOK</option>
          <option value="take a walk">take a walk</option>
          <option value="buy a bagel">buy a bagel</option>
       </select>

     <br/>
     to submit your choices, press this button 
    <input type="submit" value="submit choice">

  </form>
 </body>
</html>

Upvotes: 1

Views: 73

Answers (3)

davidkonrad
davidkonrad

Reputation: 85528

You should print_r $_POST and see what field names $_POST contains.

On my computer, $_POST holds

Array ( [do_today] => buy a bagel )

eg the <select> name do today is interpredted as do_today

so I'll suggest

echo $_POST['do_today'];

Upvotes: 1

Awlad Liton
Awlad Liton

Reputation: 9351

You should not use spaces in tag name.

<html>
  <body>

    <form action="planner.php" method="post">
       what would you like to do today?
       <select name="do_today">
          <option value="drink coffee" >drink coffee</option>
          <option value="read book"  selected="selected">READ BOOK</option>
          <option value="take a walk">take a walk</option>
          <option value="buy a bagel">buy a bagel</option>
       </select>

     <br/>
     to submit your choices, press this button 
    <input type="submit" value="submit choice">

  </form>
 </body>

in planner.php get value by:

echo $_REQUEST['do_today'];

it will give you the value of selected option not the name of the option. if you select <option value="read book" selected="selected">READ BOOK</option>

then it will give you read book not READ BOOK

It should make sense :)

Upvotes: 1

Realit&#228;tsverlust
Realit&#228;tsverlust

Reputation: 3953

Well ... if you just want the value, its pretty easy:

$var = $_POST['dotoday'];

i would always recommend no spaces in names. Just annoys.

Im pretty sure i misunderstood you, because i always misunderstand people. So ask if something isnt clear or if my answer wasnt fully answering your question.

Upvotes: 2

Related Questions