0
<?php
if(isset($_POST['additem_button'])) 
$sub_category_p = isset($_POST['sub_category']);
if($main_category_p == "Choose..."){ $main_category_p = ""; }
echo "$main_category_p";

?>



                              <select name = 'main_category'>
                                  <option value="Choose one">Choose...</option>
                                  <option value="Apparel">Apparel</option>
                                  <option value="Home Decor">Home Deco</option>
                                  <option value="Beauty">Beauty</option>
                            </select>

it doesnt echo out any thing. no matter which option i chlice in select.

5
Contributors
4
Replies
5
Views
4 Years
Discussion Span
Last Post by Bob Hensley
0

As urtrivedi says, please post some more code.
You are saying that $main_category_p doesn't display anything but your code does not show where this variable is set.

1

Unless you are assigning the $_POST['main_category'] to a variable called $main_category_p somewhere that we can't see, then you won't get anything echo'd out. You either need to assign the posted value to your new variable $main_category_p = $_POST['main_category'];or you should be using

if($_POST['main_category'] == "Choose..."){ $_POST['main_category'] = ""; }
echo "$_POST['main_category']";
2

Assuming $main_category_p is being set to $_POST['main_category']:

The problem is:

if($main_category_p == "Choose...")

The value attribute of the OPTION is what is sent via HTTP POST. Not the actual display text you're showing the client. That's arbitrary and has no implications beyond presentation. So, given your default value is actually "Choose one", that't what you'd want to be checking for in that IF statement.

Unfortunately, HTML5 doesn't support a placeholder element for SELECT statements. So avoiding this problem entirely is not possible. What I prefer to do, however, is to give the placeholder OPTION an empty value. Then, in PHP, check for a non-empty value. If it's empty, then the user chose the placeholder.

The HTML

<select name="main_cat">
  <option value="">Choose...</option>
  <option value="Apparel">Apparel</option>
  <option value="Home Decor">Home Deco</option>
  <option value="Beauty">Beauty</option>
</select>

PHP

$selected = $_POST['main_cat'];

if ( ! empty ($selected))
  echo $selected;
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.