Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
PHP
<form action="search.php" method="post">
<?php
$select_query=          "Select distinct category from books";
$select_query_run =     mysql_query($select_query);
$select_query_array=   mysql_fetch_array($select_query_run) ;
 echo "<select name='category'>";
while ($select_query_array=   mysql_fetch_array($select_query_run) )
{
//dynamic dropdown list and values are like cat1,cat2,cat3 and cat4

   echo "<option value='' >".htmlspecialchars($select_query_array["category"])."</option>";
}

echo "</select>"; 
 ?>
   <<input type="text" name="searchvalue" size="5"  id="searchfield" title="searchfield" onFocus="clearText(this)"/>
     <input type="submit" name="submit" value="" alt="Search" id="searchbutton" title="Search" />
</form><>


SEARCH.PHP
I am just trying to print what value actually user selected from drop drown list.. I tried below code to check even I tried with SESSION but its not printing any value please anyone help me.
PHP
<?php
echo $_POST['category'];
?>
Posted

As Balboos was trying to point out, what is being displayed on the dropdownlist is as it is for display purpose. The actual value that got sent by post to the server is contained in the value attribute of the option tag. The display text and value may or may not be the same. e.g.
echo "<option value='".htmlspecialchars($select_query_array["category"])."' >".htmlspecialchars($select_query_array["category"])."</option>";

And one of the statement is redundant, see the full code:
PHP
<?php
$select_query=          "Select distinct category from books";
$select_query_run =     mysql_query($select_query);
//$select_query_array=   mysql_fetch_array($select_query_run) ;
echo "<select name='category'>";
while ($select_query_array=   mysql_fetch_array($select_query_run) )
{
//dynamic dropdown list and values are like cat1,cat2,cat3 and cat4

   echo "<option value='".htmlspecialchars($select_query_array["category"])."' >".htmlspecialchars($select_query_array["category"])."</option>";
}

echo "</select>";
 ?>
}

Finally, you should consider switching to mysqli extension as mysql extension has been deprecated[^].
 
Share this answer
 
v4
Comments
Member 11406049 20-Feb-15 12:28pm    
thank you very much.. your are awesome....
Well -
XML
echo "<option value='' >".htmlspecialchars($select_query_array["category"])."</option>";
}


has set the value of all of your options to nothing via value=''.

Your ECHO request is for the value, not the displayed string.

so - you see exactly that: nothing.

To keep it simple for you, as you create your options, create them with the 'value' set to equal to the display value.

There are ways to pick up the display values, but that should wait until you learn a lot more about the basics.
 
Share this answer
 
Comments
Member 11406049 20-Feb-15 11:53am    
but that code is selecting the values from database and printing in the drowp down menu. at the top before while() loop I gave name for <select> right.. hmmm.. really I am beginner in php. so please help me with this. my task is I need to print the valued based on category selected from dropdown list.
W Balboos, GHB 20-Feb-15 12:01pm    
You are asking for the value. The stuff showing in the dropdown menu is NOT the value returned by your form. All of the values items from a form with a name are sent to the form's target.

I'll say it again: the stuff you see on the dropdown are NOT the values - they're just for the user interface. The value is set with the value='something' for each option.

Try the tutorials at w3schools. This is not a php issue. This is understanding how HTML forms work and how the HTML elements on the forms work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900