954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to selected value of dropdown box

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

adityamadhira
Newbie Poster
23 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

Use that code.
If you want to get text that's in the 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.

Robert Rusu
Newbie Poster
2 posts since Jul 2011
Reputation Points: 10
Solved Threads: 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.

adityamadhira
Newbie Poster
23 posts since Feb 2012
Reputation Points: 10
Solved Threads: 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.

hag++
Junior Poster
197 posts since Jan 2010
Reputation Points: 34
Solved Threads: 31
 

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


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

}

?>






<?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;
}

?>

I tried but not working

adityamadhira
Newbie Poster
23 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

your dropdown is outside the form, move that inside the form

vaultdweller123
Posting Pro
554 posts since Sep 2009
Reputation Points: 42
Solved Threads: 75
 

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

cigoL..:)
Newbie Poster
19 posts since Jul 2011
Reputation Points: 10
Solved Threads: 2
 

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.

broj1
Posting Whiz
359 posts since Jan 2011
Reputation Points: 29
Solved Threads: 43
 

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 tag inside the PHP tag :)

cigoL..:)
Newbie Poster
19 posts since Jul 2011
Reputation Points: 10
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: