0

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

6
Contributors
8
Replies
12
Views
5 Years
Discussion Span
Last Post by cigoL..:)
0

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.

Edited by Robert Rusu: n/a

0

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.

0

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.

Edited by hag++: n/a

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

Edited by Dani: Formatting fixed

0

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

Edited by cigoL..:): typographic error and i forgot the other end tag :)

0

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.

0

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 :)

This topic has been dead for over six months. 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.