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

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

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;
?>

<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.

<?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>";


?>

<?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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.