...

View Full Version : problem with listbox



Daxini
05-05-2009, 06:45 AM
hi, I have a php function and html code like that:



function func_availability($dbc, $day)
{ $sql = "SELECT * FROM tblavail";
$rs = odbc_exec($dbc, $sql);
if(!$rs)
exit("Error in SQL");
while(odbc_fetch_row($rs))
{ $TID=odbc_result($rs,"ID");
$Tday=odbc_result($rs,"day");
if($day==$TID)
echo("<option value=\"$TID\"selected=\"selected\">$Tday
</option>");
else if($TID=="4")
echo("<option value=\"$TID\" selected=\"selected\">$Tday
</option>");
else
echo("<option value=\"$TID\">$Tday</option>");
}

html code:-
<label for="dayvailable" class="required">Day Available:</label><br />
<select name="day" id="day" tabindex="3" title="day"><?php func_availability($dbc, $_SESSION['day']) ?></select><br />


and a database table :-
ID Type
1 mon
2 tues
3 wed
4 thur
5 fri
6 sat
7 sun

actullay what i want to do with this function is that i want to display database table data in listbox on form and and I want to set default value of listbox to thursday(4) and when user select another value, submit the form, if an error occurs then listbox should be displayed with the selected value.

this function is displaying default value in listbox and when i select the value from listbox below thursday and submit the form and if error occurs on the form, it is displayed with the selected value in listbox but when i select value from listbox before thursday and submit the form and if error occurs on the form, it is displayed with the default value in listbox.

when i view the source code of form after submission i found that 2 values are set selected = selected and what i need is only one should be selected means if user select a value then user's selected value should be set selected otherwise default value should be selected.

abduraooft
05-05-2009, 10:48 AM
Try


if(isset($_POST[''day]) && $_POST['day']==$TID)
echo("<option value=\"$TID\"selected=\"selected\">$Tday
</option>");
else if($day==$TID)
echo("<option value=\"$TID\"selected=\"selected\">$Tday
</option>");
else if($TID=="4")
echo("<option value=\"$TID\" selected=\"selected\">$Tday
</option>");
else
echo("<option value=\"$TID\">$Tday</option>");



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum