Jump to content

[SOLVED] Drop down box (Yes or No) with default selected from the database


$username

Recommended Posts

Hello All,

 

Here is the code that I am using and I want to know if yes and no  must be stored in the  database to get the selected one from the database to be populated.

 

<p>
  <td><font color="blue">Paid:</font></td> 
<td><select name="Paid">
	<option selected="<? echo $formVars["Paid"]; ?>"><? echo $formVars["Paid"]; ?></option>
	<option value="No">No</option>
	<option value="Yes">Yes</option>
</select><big></big></td>
</tr>
</p>

 

Here is what happens now

yesno.JPG

Link to comment
Share on other sites

Here is the code that I am using and I want to know if yes and no  must be stored in the  database to get the selected one from the database to be populated.

 

<p>
  <td><font color="blue">Paid:</font></td> 
<td><select name="Paid">
	<option selected="<? echo $formVars["Paid"]; ?>"><? echo $formVars["Paid"]; ?></option>
	<option value="No">No</option>
	<option value="Yes">Yes</option>
</select><big></big></td>
</tr>
</p>

 

Here is what happens now

yesno.JPG

Link to comment
Share on other sites

You could do something like this:

 

<?
$yes_sel = ""; $no_sel = "";
if ($formVars['Paid'] == "No") {
   $no_sel = "selected='selected'";
} else {
   $yes_sel = "selected='selected'";
} // end if

echo <<<HTMLHERE
<select name="Paid">
<option value="No" $no_sel>No</option>
<option value="Yes" $yes_sel>Yes</option>
</select>
HTMLHERE;
?>

Link to comment
Share on other sites

<p>
  <td><font color="blue">Paid:</font></td> 
<td><select name="Paid">
<?php
$no_opt = '<option value="No" ';
$yes_opt = '<option value="Yes" ';
if ($formVars["Paid"] == "No") 
     $no_opt .= 'selected=selected';
else
     $yes_opt .= 'selected=selected';

$no_opt .= '>';
$yes_opt .= '>';
?>
	<?php echo $no_opt;?>No</option>
	<?php echo $yes_opt;?>Yes</option>
</select><big></big></td>
</tr>
</p>

 

Sort of sloppy but will do the trick.

Link to comment
Share on other sites

<?php

//$result['Paid'] is the db value

echo "<select name=\"Paid\">";
echo "    <option value=\"No\"".(($result['Paid']=='No')?' selected="selected"':'').">No</option>";
echo "    <option value=\"Yes\"".(($result['Paid']=='Yes')?' selected="selected"':'').">Yes</option>";
echo "</select>";


?>

Link to comment
Share on other sites

<?php

 

//$result['Paid'] is the db value

 

echo "<select name=\"Paid\">";

echo "    <option value=\"No\"".(($result['Paid']=='No')?' selected="selected"':'').">No</option>";

echo "    <option value=\"Yes\"".(($result['Paid']=='Yes')?' selected="selected"':'').">Yes</option>";

echo "</select>";

 

 

?>

 

I am using this code to update data that is in the database. What I would like is the code to pull what the database has, and then use it in the drop down box. But I do not want the drop down to have 2 yes or no in the drop down. But I still would like the drop down box to populate the yes or no that the database has listed.

 

Thank you,

Brett

Link to comment
Share on other sites

I used this and it seems to work ok.

 

  <td><font color="blue">Paid:</font></td> 
<td><select name="Paid">
<?php
$no_opt = '<option value="No" ';
$yes_opt = '<option value="Yes" ';
if ($formVars["Paid"] == "No") 
     $no_opt .= 'selected=selected';
else
     $yes_opt .= 'selected=selected';

$no_opt .= '>';
$yes_opt .= '>';
?>
	<?php echo $no_opt;?>No</option>
	<?php echo $yes_opt;?>Yes</option>
</select><big></big></td>
</tr>
</p>

 

 

Thanks guys.

Brett

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.