Jump to content

Drop Down menu Value should stay selected when selected


southpawnigeria

Recommended Posts

Hello peeps,

 

I have a drop down menu which i want to stay selected when a value has bn chosen. I have searched through thw different ones but have not found one that supports my coding.

 

Here is the drop down

<select name="clients" class="DEPENDS ON form BEING 2" id="clients">
        <option value="0" selected="<?php echo $_POST['clients']; ?>">Select Company Name:</option>
        <?php 
do {  
?>
        <option value="<?php echo $row_clients['clientsid']?>" selected="<?php echo $_POST['clients']; ?>"><?php echo ucfirst($row_clients['name']);?></option>
        <?php
} while ($row_clients = mysql_fetch_assoc($clients));
$rows = mysql_num_rows($clients);
  if($rows > 0) {
      mysql_data_seek($clients, 0);
  $row_clients = mysql_fetch_assoc($clients);
  }
?>
      </select>

 

Please any insights on the issue wud be very appreciated

Link to comment
Share on other sites

I suggest separating your logic from your HTML. Makes things much easier, IMO. Also, I would revise the variable naming. The select list is currently named "clients", but it is a list to select ONE client, so it should logically be called "client". That way on the processing page you are referncing "$_POST['client']" which makes more sense since you are only expecting one client.

 

This would go in the head or logic file

<?php
$client_options = '';
while ($client_row = mysql_fetch_assoc($clients))
{
    $selected = ($client_row['clientsid'] == $_POST['clients']) ?  ' selected="selected"': '';
    $client_options .= "    <option value=\"{$client_row['clientsid']}\"{$selected}>".ucfirst($client_row['name'])."</option>\n";
}
?>

 

This goes in the bottom of the document or in the output script

<select name="clients" class="DEPENDS ON form BEING 2" id="clients">
    <option value="">Select Company Name:</option>
    <?php echo $client_options; ?>
</select>

Link to comment
Share on other sites

Of course there is a reason, but I'm not understanding you. Are you saying that the "Select Company Name" is not included in the select list? If so, I don't see how since it is hard coded - unless there is some JavaScript which manipulates that list.

 

The other possible problem that you are trying to describe may be that the 1st company from the database is not included in the select list. If that is the problem, I suspect you implement the code I provided incorrectly and are running a mysql_fetch of some sort before the loop starts - which would "use up" the first record.

 

In any case a more detailed explanation of the problem and a link to the page or the current code would be helpful.

Link to comment
Share on other sites

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.