Forum Moderators: coopster

Message Too Old, No Replies

items in <option> not showing

         

ttkt

4:57 pm on Jul 27, 2005 (gmt 0)

10+ Year Member



hi, i have the combo boxes, the options are retrieved from the database directly.

// first combo box
<select name="queryTypeCombo" onchange=submitForm()>
<option>-- Please select --</option>
<option value = 1>OnLoan</option>
<option value = 2>Availability</option>
<option value = 3>Missing</option>
<option value = 4>Condemned</option>
</select>

i did a document.form.submit() to get the $_POST['queryTypeCombo'] and store it into a session.

after the onchange() is executed in first combo box, second combo box will be displayed. the option values in the second combo box is dependable on the options selected in first combo box.

// onchange function
function submitForm()
{
document.searchMenuForm.submit();
}

// second combo box
<select name="queryAssetTypeCombo" onchange=submitForm()>
<option>-- Please select --</option>
<option value = Laptop>Laptop</option>
<option value = Olympus Digital Recorder>Olympus Digital Recorder</option>
</select>

my problem is after i have selected the option e.g. OnLoan or Laptop in either the 1st or 2nd combo box, -- Please select -- will always be displayed instead of OnLoan or Laptop in the combo boxes. may i know what has gone wrong? is it because i did a submitForm() that causes the combo boxes to refresh back to -- Please select --? another problem is the options in the second combo box is empty after i have selected Laptop.

how do i display the selected option correctly in the combo boxes?

Stu_Rogers

12:52 pm on Jul 28, 2005 (gmt 0)

10+ Year Member



Not sure that my way is the correct/accepted or most efficient way but...

You need to echo the word "selected" with in the html option tag for the current option.

For example,

echo "<option value=3 ";
if (queryTypeCombo=3) {
echo "selected";
}
echo ">Missing</option>";

ttkt

4:10 am on Jul 29, 2005 (gmt 0)

10+ Year Member



hi Sut_Rogers, i have a function to retrieve the result from database. may i know how do i go about it? i have tried inserting but it gives me strange result, thanks.

function retrieveQueryType() {

$query = "select * from ims_status";
$result = mysql_query($query);

echo "<select name=\"queryTypeCombo\" onchange=submitForm()>\n";
echo "<option>-- Please select --</option>\n";

//Now fill the table with data
while ($row = mysql_fetch_array($result))
{
$statusID = $row['statusID'];
$type = $row['type'];

}
}
echo "</select>\n";
}

ttkt

4:12 am on Jul 29, 2005 (gmt 0)

10+ Year Member



sorry, below is the correct codes.

function retrieveQueryType() {

$query = "select * from ims_status";
$result = mysql_query($query);

echo "<select name=\"queryTypeCombo\" onchange=submitForm()>\n";
echo "<option>-- Please select --</option>\n";

//Now fill the table with data
while ($row = mysql_fetch_array($result))
{
$statusID = $row['statusID'];
$type = $row['type'];
echo "<option value = $statusID>$type</option>\n";
}
//echo "</select>\n";
echo ">$type</option>";
}

vbbnn

7:30 am on Jul 29, 2005 (gmt 0)

10+ Year Member



"<option value = Laptop>Laptop</option>
<option value = Olympus Digital Recorder>Olympus"

Try to take away the spaces before and after "=", like these:

<option value=Laptop>Laptop</option>
<option value="Olympus Digital Recorder">Olympus

L

dreamcatcher

8:10 am on Jul 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



vbbn, the spaces won`t make any difference.

I think the problem lies here:


while ($row = mysql_fetch_array($result))
{
$statusID = $row['statusID'];
$type = $row['type'];
echo "<option value = $statusID>$type</option>\n";
}
//echo "</select>\n";
echo ">$type</option>";
}

The final echo, what is that doing exactly? Why have you commented out the closing select? This is possibly the problem. Try the following:


while ($row = mysql_fetch_array($result))
{
echo '<option value="'.$row['statusID'].'">'.$row['type'].'</option>';
}
echo '</select>';

}

dc

ttkt

3:01 pm on Jul 30, 2005 (gmt 0)

10+ Year Member



hi dreamcatcher, the comment was accidentally commented out because i was doing some debugging there.

yup, i have tried the code: echo '<option value="'.$row['statusID'].'">'.$row['type'].'</option>';

result: last item is set as default when page is loaded. i have other combo boxes, the values are dependent on the first combo box. this line of code doesn't seem to work well.