My code of drop down box is

<select name="dname">
<?php
$data = @mysql_query("select * from Department");
while ($row = mysql_fetch_assoc($data))
{
$ID = $row['dept_ID'];
$dname = $row['department_name'];
   echo  "<option value=$ID name=not>$dname \n";
}
?>
    </select>

I retrieving department name into dropdown box.. ID - is department ID.....when user click submit i need to get selected department ID from drop down box(not value)....

I am beginner in php ....thank you

Recommended Answers

All 8 Replies

Use that code.
If you want to get text that's in the <option></option> you can do that via javascript on just

select department_name from Department where dept_ID = $_POST['dname']

And the code should look like this

<select name="dname">
<?php
$data = @mysql_query("select * from Department");
while ($row = mysql_fetch_assoc($data))
{
$id = $row['dept_ID'];
$dname = $row['department_name'];
   echo '<option value='.$id.'>'.$dname.'</option>'."\n";
}
?>
</select>

Personal tip, instead of dname call it dep_id.

My problem is to get id of the department what user selects not department name....
after selecting department name then user will hit submit button at that time i have to catch dept_id of the selected department name.

I hope you got it.

When the form is submitted using the $_POST array should get you the value of the selected option.

Using your example you could use

$_POST['dname']

which will return the value for whatever option is selected. Since you are setting the department ID as the value, that is what you will receive.

<?php
 include("Database.php");
       $val =new DB;
       $val -> connect();
       ?>


<select name="dname">
<?php
$data = @mysql_query("select * from Department");
while ($row = mysql_fetch_assoc($data))
{
$id = $row['dept_ID'];
$dname = $row['department_name'];
   echo '<option value='.$id.'>'.$dname.'</option>'."\n";

}

?>

 </select>

 <form action= "test.php" method="post">


 <input type="submit" name="sub" value ="Hi"/>

 <?php
if (isset($_POST['sub']))
{ 
$retrieve = mysql_query("select department_name from Department where dept_ID ='" .$_POST['dname']. "'"); 

         while($info = mysql_fetch_array( $retrieve )) 
         {
             $value = $info['user_id'] ;
             echo $value;

         }

    echo $value;
}

?>
 </form>

I tried but not working

your dropdown <select> is outside the form, move that inside the form

Try This,

<?php 
$data = @mysql_query("select * from Department");
echo "<select name=dname>";
while ($row = mysql_fetch_assoc($data))
{
$ID = $row['dept_ID'];
$dname = $row['department_name'];
   echo  " <option value=".$ID. "name=not>".$dname."\n</option>";
}
echo "<select>";


$deptname= $_POST['dname'];

// do codes here..
?>

hope it helps :)
try putting select inside the form

Also set action attribute to '#' so the form gets submitted to itself.

<form action= "#" method="post">

.
And yes, select element should be between <form> and </form> tags.

Try this

<?php
$data = @mysql_query("select * from Department");
echo "<select name="dname">";
while ($row = mysql_fetch_assoc($data))
{
$id = $row['dept_ID'];
$dname = $row['department_name'];
   echo '<option value='.$id.'>'.$dname.'</option>'."\n";
}
echo "</select>";
?>

Put <select> tag inside the PHP tag :)

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.