<?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.

Recommended Answers

All 4 Replies

post complete code

Member Avatar for Zagga

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.

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']";

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;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.