i was trying to configure about how to work with <select> tag, when i was stuck with this problem: when i choose the <select> tag with PHP, i just want that that value of the <option> tag on the <select> tag will be the value selected after i submit the form. but the thing is, it will go back to the default value. Just try to look at the code:

<select name="project">
   	<option value="">-Select one-</option> -->
        <?php 
        	while($proj = mysql_fetch_array($qryS)){
			$id = $proj['id'];
			$project = $proj['project'];
			
			$i = $i + 1;
			echo "<option value=\"$i\"";
			echo ($i == $id) ? "SELECTED":"";
			echo ">" . $project . "</option>";
			
			}
			
			?>      
    </select>

so this is my code. I try to fetch some data from my database.

anyone can help. I really need your help guys. thanks

Hi,
This is very simple. I can help You. Just follow the code given below

$con=mysql_connect("localhost","username","password");
		if (!$con)
		die("Couldn't connect to MySQL");
		mysql_select_db("databasename",$con)
		or die("Couldn't open admin: ".mysql_error());
		$q1=mysql_query("SELECT <fieldname> FROM <tablename>");
		?>
        <select name="<fieldname>">
		<?
	    while($row=mysql_fetch_array($q1))
		{
		?>
		<option value="<?php echo $row['<fieldname>'];?>">
        <?php 
		echo $row['<fieldname>'];
		
		 ?>
	    </option>
	    <?
		 }
	    ?>
		</select>

Just remove username,password, databasename, tablename,fieldname as you have.
I hope this will work

Hi,
This is very simple. I can help You. Just follow the code given below

    $con=mysql_connect("localhost","username","password");
    if (!$con)
    die("Couldn't connect to MySQL");
    mysql_select_db("databasename",$con)
    or die("Couldn't open admin: ".mysql_error());
    $q1=mysql_query("SELECT <fieldname> FROM <tablename>");
    ?>
    <select name="<fieldname>">
    <?
    while($row=mysql_fetch_array($q1))
    {
    ?>
    <option value="<?php echo $row['<fieldname>'];?>">
    <?php 
    echo $row['<fieldname>'];

     ?>
    </option>
    <?
     }
    ?>
    </select>

Just remove username,password, databasename, tablename,fieldname as you have.
I hope this will work

i better try this now. thanks

i was trying to configure about how to work with <select> tag, when i was stuck with this problem: when i choose the <select> tag with PHP, i just want that that value of the <option> tag on the <select> tag will be the value selected after i submit the form. but the thing is, it will go back to the default value. Just try to look at the code:

<select name="project">
   	<option value="">-Select one-</option> -->
        <?php 
        	while($proj = mysql_fetch_array($qryS)){
			$id = $proj['id'];
			$project = $proj['project'];
			
			$i = $i + 1;
			echo "<option value=\"$i\"";
			echo ($i == $id) ? "SELECTED":"";
			echo ">" . $project . "</option>";
			
			}
			
			?>      
    </select>

so this is my code. I try to fetch some data from my database.

anyone can help. I really need your help guys. thanks

thank you for this code. I tried it and the result is still the same. I just want that the value of the selected option will show in the <select> tag when i submit the form. it will stay in the same as i chose from the selected option.

hi there..
i almost got the same problem.
i have 3 drop down menus in my page.
i was able to populate the first drop down menu.
the 2nd and 3rd drop down menu was not successfully
populated.
hope you can help me.
my query goes:
$query = "SELECT table1.col1, table2.col2
FROM table1, table2"
the first drop down menu would work, not in the succeeding
drop down menu.

Thanks,
Tintin

================================================

Hi,
This is very simple. I can help You. Just follow the code given below

$con=mysql_connect("localhost","username","password");
		if (!$con)
		die("Couldn't connect to MySQL");
		mysql_select_db("databasename",$con)
		or die("Couldn't open admin: ".mysql_error());
		$q1=mysql_query("SELECT <fieldname> FROM <tablename>");
		?>
        <select name="<fieldname>">
		<?
	    while($row=mysql_fetch_array($q1))
		{
		?>
		<option value="<?php echo $row['<fieldname>'];?>">
        <?php 
		echo $row['<fieldname>'];
		
		 ?>
	    </option>
	    <?
		 }
	    ?>
		</select>

Just remove username,password, databasename, tablename,fieldname as you have.
I hope this will work

hi there..
i almost got the same problem.
i have 3 drop down menus in my page.
i was able to populate the first drop down menu.
the 2nd and 3rd drop down menu was not successfully
populated.

Well you have only provided one query there so that would be why only one menu is working. Does your script look something like the following:

$con=mysql_connect("localhost","username","password");
if (!$con) die("Couldn't connect to MySQL");
mysql_select_db("databasename",$con) or die("Couldn't open admin: ".mysql_error());
$q1=mysql_query('SELECT table1.col1, table2.col2 FROM `table1`, `table2`');
echo '<select name="<fieldname>">';
while($rowa=mysql_fetch_array($q1))
    {
    echo '<option value="'.$rowa['<fieldname>'].'">';
    echo $rowa['<fieldname>'].'</option>';
    }
echo '</select>';


$q1=mysql_query('SELECT table1.col1, table2.col2 FROM `table1`, `table2`');
echo '<select name="<fieldname>">';
while($rowb=mysql_fetch_array($q1))
    {
    echo '<option value="'.$rowb['<fieldname>'].'">';
    echo $rowb['<fieldname>'].'</option>';
    }
echo '</select>';


$q1=mysql_query('SELECT table1.col1, table2.col2 FROM `table1`, `table2`');
echo '<select name="<fieldname>">';
while($rowc=mysql_fetch_array($q1))
    {
    echo '<option value="'.$rowc['<fieldname>'].'">';
    echo $rowc['<fieldname>'].'</option>';
    }
echo '</select>';

Note: In the above example replace <fieldname> with the real column name.
And also, just as an opinion, I find the examples in previous posts to be very basic and very non-php in a way. I believe the echo/print functions are the way to go.

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.