Jump to content

Recommended Posts

I've had a search around and I've not been able to find anything specifically relating to this and been unable to piece anything together.

 

I'm wanting to have a dropdown box in my form populated with options within the usual select tag but also have whatever option was saved originally in my MySQL table as the selected option.

 

I have at the moment:

 

table name: combo

field name: dropdown

This is the only field in the table that I set up for testing this.

 

$query = mysql_query("SELECT dropdown FROM combo") or die(mysql_error());

echo "<form action='action' method='post'>";
echo "<select name='option'>";
while ($nt = mysql_fetch_array($query)) {
echo "<option value=''>".$nt['dropdown']."</option>";
}
echo "</select><br />";
echo "<input type='submit' value='Submit'>";
echo "</form>";

 

But this is obviously only placing the one option in the dropdown which is of course, pointless.

 

Could anyone please point me in the right direction?

Hey, i think this is something that your talking about.

 

From what i understand it is something that i have actually needed to do quite often.

 


<form name="form1">
        Select your action: 
        <select name="option_show">
          
           <?php
           $saved_orig = "whatever option was saved originally in my MySQL table ";
           $query = mysql_query("SELECT dropdown FROM combo") or die(mysql_error());
           while ($nt = mysql_fetch_assoc($query))
           {
     echo "<option value=\"$dropdown\""; if ($dropdown == $saved_orig){ echo "selected";} echo">".$nt['dropdown']."</option>";
           }
           ?>
       </select>
</form>

 

hope it helps

Thanks for the swift reply Saint.

 

After trying to wrap my brain round that, I have assumed that I should have a field in a table somewhere that includes all (4 in my case) of the options for the dropdown which is seperate to the actual saved_orig field.

 

So I've done that and edited my code accordingly and whilst the dropdown is being populated correctly, it's not selecting the right option.

 

table(1) name: combo_selected

field name(containing selected item): selected

 

table(2) name: combo_options

field name(containing 4 options): options

 

<?php

$selected = mysql_query("SELECT selected FROM combo_selected");
$options = mysql_query("SELECT options FROM combo_options");

while ($nt = mysql_fetch_assoc($options))
{
echo "<option value=\"$options\""; if ($options == $selected){ echo "selected";} echo">".$nt['options']."</option>";
}

?>

 

I'm obviously missing something!

I think the below may work. from the off set it does not seem like you are actually getting the value from the combo_selected table.

 

Also, im assuming the value in combo_selected is equal to one of the values in the combo_options table?

 

<?php

$selected_query = mysql_query("SELECT selected FROM combo_selected");
$row = mysql_fetch_assoc($selected_query)
$selected = $row['selected'];

$options = mysql_query("SELECT options FROM combo_options");

while ($nt = mysql_fetch_assoc($options))
{
echo "<option value=\"$options\""; if ($options == $selected){ echo "selected";} echo">".$nt['options']."</option>";
}

?>

Also, i think there is still going to be alot of work done here as you will have to get a way to link a unique record in the `combo_selected` table to specific user who is viewing the site (if i am thinking right about what it is you want). but i think that can come once you have the concept right. Sorry, but i leaving the office now and will only be back tomorrow.

This is similar to one of my pieces of code.

try:

	
$query = mysql_query("SELECT dropdown FROM combo") or die(mysql_error());

echo '<form action="action" method="post">';


echo '<select name="option_show">';  
echo '<option>Choose Option</option>';

while($nt = mysql_fetch_array($query))
{  
  echo '<option value="'.$nt[0].'"'.($nt[0]==$dropdown ? ' selected="selected"' : '').'>'.$nt[0].'</option>';
   
}  

echo '</select><br />';
echo '<input type="submit" value="Submit">';
echo '</form>';

 

You have the echo '<option>Choose Option</option>'; part just incase a record hasn't been selected previously.

 

Hope this helps

 

No joy.

I thought your suggestion had worked for a moment New Coder but it was only because of the ORDER BY you originally posted that put the relevant option to the top of the dropdown.

 

I'll leave this for today and come back to it with a fresh pair of eyes tomorrow. Thanks for your suggestions today!

actually I have missed a section from my example.

you will need to query to find orig option selected from the table you have stored the form data.

 

like:

$query1 = mysql_query("SELECT selected FROM combo_selected") or die(mysql_error());

$num = mysql_num_rows ($query1);
if ($num !=0)
{			
#retrieve data
$row = mysql_fetch_array( $query1 );
$previous_option = $row["selected"];
}
else
{ exit();}

$query = mysql_query("SELECT dropdown FROM combo") or die(mysql_error());

echo '<form action="action" method="post">';


echo '<select name="option_show">';  
echo '<option>Choose Option</option>';

while($nt = mysql_fetch_array($query))
{  
 echo '<option value="'.$nt[0].'"'.($nt[0]==$previous_option ? ' selected="selected"' : '').'>'.$nt[0].'</option>';
  
}  

echo '</select><br />';
echo '<input type="submit" value="Submit">';
echo '</form>';

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.