| | |
dynamic dropdown
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
try this code it works for me
PHP Syntax (Toggle Plain Text)
<?php $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="forum"; // Database name $tbl_name="test_mysql"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM test_mysql"; $result=mysql_query($sql); ?> <form name="form1" method="post" action=""> <select name="Names" id="Names"> <?php while($rows=mysql_fetch_array($result, MYSQL_ASSOC)) { echo"<option>". $rows['name']."</option>"; //to display names from database in field called name } mysql_close(); ?> </select> </form>
If you find this useful you can add to my reputation
You can do something like tirivamwe has mentioned. Or you can use a function so that you can reuse the code whenever you want.
php Syntax (Toggle Plain Text)
<?php $conn=mysql_connect("localhost","root"); mysql_select_db("test"); function dropdown($colname,$tablename){ $query="select ".$colname." from ".$tablename; $result=mysql_query($query); $option=""; while($row=mysql_fetch_array($result)){ $option.="<option value=".$row[0].">".$row[0]."</option>"; } return $option; } ?> <html> <body> <form> <select name='test'> <?php echo $options=dropdown("Column_name","Table_name"); //the column name of table_name which you want to display in the options ?> </select> <select name='test1'> <?php echo $options=dropdown("Column_name1","Table_name2"); ?> </select> </form> </body> </html>
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
•
•
Join Date: Nov 2007
Posts: 183
Reputation:
Solved Threads: 5
hi
this works but how to get the dropwown value in the next page..
i tried with $id=$_REQUEST['id']; but this is not working
<form method="get" action="sort2.php">
<?php
$hostname = "localhost";
$username = "";
$password = "";
$dbid = "";
$link=mysql_connect($hostname, $username, $password);
mysql_select_db($dbid) or die("unable to connect");
$sql="SELECT id,name FROM personal";
$result=mysql_query($sql);
?>
<table>
<tr>
<td>Splendor Id</td><td><select name="Names">
<?php
while($rows=mysql_fetch_array($result))
{
echo"<option value=".$rows[0].">". $rows[1]."</option>";
}
mysql_close();
?>
</select></td>
</tr>
<tr>
<td>From</td><td><input type="text" name="from"></td>
<td>To</td><td><input type="text" name="to"></td>
<td><input type="submit" name="submit" value="submit"></td></tr>
</table>
</form>
this works but how to get the dropwown value in the next page..
i tried with $id=$_REQUEST['id']; but this is not working
<form method="get" action="sort2.php">
<?php
$hostname = "localhost";
$username = "";
$password = "";
$dbid = "";
$link=mysql_connect($hostname, $username, $password);
mysql_select_db($dbid) or die("unable to connect");
$sql="SELECT id,name FROM personal";
$result=mysql_query($sql);
?>
<table>
<tr>
<td>Splendor Id</td><td><select name="Names">
<?php
while($rows=mysql_fetch_array($result))
{
echo"<option value=".$rows[0].">". $rows[1]."</option>";
}
mysql_close();
?>
</select></td>
</tr>
<tr>
<td>From</td><td><input type="text" name="from"></td>
<td>To</td><td><input type="text" name="to"></td>
<td><input type="submit" name="submit" value="submit"></td></tr>
</table>
</form>
•
•
•
•
hi
this works but how to get the dropwown value in the next page..
i tried with $id=$_REQUEST['id']; but this is not working
<form method="get" action="sort2.php">
<?php
$hostname = "localhost";
$username = "";
$password = "";
$dbid = "";
$link=mysql_connect($hostname, $username, $password);
mysql_select_db($dbid) or die("unable to connect");
$sql="SELECT id,name FROM personal";
$result=mysql_query($sql);
?>
<table>
<tr>
<td>Splendor Id</td><td><select name="Names">
<?php
while($rows=mysql_fetch_array($result))
{
echo"<option value=".$rows[0].">". $rows[1]."</option>";
}
mysql_close();
?>
</select></td>
</tr>
<tr>
<td>From</td><td><input type="text" name="from"></td>
<td>To</td><td><input type="text" name="to"></td>
<td><input type="submit" name="submit" value="submit"></td></tr>
</table>
</form>
And Please, next time you post your code, place it within [ code] ... [ / code] tags !
Last edited by nav33n; Dec 19th, 2007 at 7:10 am.
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
![]() |
Similar Threads
- dynamic dropdown selection (PHP)
- Dynamic dropdown list (ASP.NET)
- Storing dynamic form values in Arrays for display & insert (PHP)
- Dropdown box in Python (Python)
- Making T.Greer's JavaScript-synthesised combobox dynamic (JavaScript / DHTML / AJAX)
- Help with dynamic menu (HTML and CSS)
Other Threads in the PHP Forum
- Previous Thread: upload files
- Next Thread: Access hidden values by javascript
| Thread Tools | Search this Thread |
301 apache api array autosuggest beginner binary broken cakephp checkbox class cms code compression cron curl data database date display dropdownlist dynamic echo email eregi error execution file files folder form forms function functions google href htaccess html httppost if...loop image include insert ip javascript joomla jquery key library limit link links login mail md5 menu mlm multiple mysql mysql_real_escape_string oop paypal pdf pdfdownload php phpvotingscript problem query radio random recursion remote screen script search searchbox server session sessions sms sorting source space sql syntax system table tutorial update upload url validator variable video volume votedown web website youtube zend






