Jump to content

Drop down box not selecting the correct default value.


Paul_Bunyan

Recommended Posts

Hello.  I am attempting to make an update page where an admin can modify existing records. 

I am running into a problem when I create my drop down lists with php and I can't seem to find the error that I am making or a thread with the same problem.

 

I am reading from an Oracle database and grabbing my record with the following code:

 

<?php
$select_doc = "SELECT total_jumps FROM mars_reports WHERE rec_id=".$rec_id;
$doc = $pubs_db->Execute($select_doc) or die($pubs_db->ErrorMsg());
$com = $pubs_db->Execute("commit") or die($pubs_db->ErrorMsg());
?>

 

As a test I have been echoing out the results to make sure that I am reading successfully:

 

<?php 

echo $doc->Fields('total_jumps');
  
?>

 

I get the expected value.  However, when I try to create my dropdown list in my form with the correct value selected by default, it does not work.  Here is the code I use w/in my form:

<tr>
    <th align="left" valign="top" scope="row">Total Jumps:  </th>
    <td align="left" valign="top">
  <select name="total_jumps" id="total_jumps">
  <option value="NA" <?php if($doc->Fields('total_jumps')=="NA") echo "selected"; ?>>Not applicable.</option>
  <option value="19" <?php if($doc->Fields('total_jumps')=="19") echo "selected"; ?>>< 20</option>
  <option value="21" <?php if($doc->Fields('total_jumps')=="21") echo "selected"; ?>>20 to 49</option>
  <option value="51" <?php if($doc->Fields('total_jumps')=="51") echo "selected"; ?>>50 to 99</option>
  <option value="101" <?php if($doc->Fields('total_jumps')=="101") echo "selected"; ?>>100 to 149</option>
  <option value="151" <?php if($doc->Fields('total_jumps')=="151") echo "selected"; ?>>150 to 199</option>
  <option value="201" <?php if($doc->Fields('total_jumps')=="201") echo "selected"; ?>>200 to 249</option>
  <option value="251" <?php if($doc->Fields('total_jumps')=="251") echo "selected"; ?> >250 to 299</option>
  <option value="300" <?php if($doc->Fields('total_jumps')== "300") echo "selected"; ?>>> 299</option>
  </select>	
  </td>
    <td align="left" valign="top">*ND</td>
  </tr>

 

 

Thank you for any insight you may have into my problem.

 

What do you mean by "it does not work"?  Does it error you?  Does it just not select the proper one?

 

Ah, sorry I wasn't specific.  It doesn't select the correct option.  It always selects, "Not Applicable."

 

.

i have fought with this in the past too.

 

try changing

echo "selected"

 

to


echo 'selected="selected"';

 

sounds stupid, but i've had that exact issue break select boxes on me.

 

I tried your solution mbeals, but I still can't get the right option to be selected.  I also tried all of the variations given by Jabop.

 

Thanks for the help so far

Here is the source that is generated:

<tr>
    <th align="left" valign="top" scope="row">Total Jumps:  </th>
    <td align="left" valign="top">
<select name="total_jumps" id="total_jumps">
     <option value="NA" >Not applicable.</option>
      <option value="19" >< 20</option>
      <option value="21" >20 to 49</option>
      <option value="51" >50 to 99</option>
  <option value="101" >100 to 149</option>
  <option value="151" >150 to 199</option>
  <option value="201" >200 to 249</option>
  <option value="251"  >250 to 299</option>
  <option value="300" >> 299</option>
  </select>	
  </td>
    <td align="left" valign="top">*ND</td>
  </tr>

it looks like there is something wrong with it processing $doc->Fields('total_jumps') or comparing it to your value.

 

<tr>
    <th align="left" valign="top" scope="row">Total Jumps:  </th>
    <td align="left" valign="top">
  <select name="total_jumps" id="total_jumps">
  <option value="NA" <?php if($doc->Fields('total_jumps')=="NA") echo "selected"; ?>>Not applicable. <?=$doc->Fields('total_jumps')?></option>
  <option value="19" <?php if($doc->Fields('total_jumps')=="19") echo "selected"; ?>>< 20 <?=$doc->Fields('total_jumps')?></option>
  <option value="21" <?php if($doc->Fields('total_jumps')=="21") echo "selected"; ?>>20 to 49 <?=$doc->Fields('total_jumps')?></option>
  <option value="51" <?php if($doc->Fields('total_jumps')=="51") echo "selected"; ?>>50 to 99 <?=$doc->Fields('total_jumps')?></option>
  <option value="101" <?php if($doc->Fields('total_jumps')=="101") echo "selected"; ?>>100 to 149 <?=$doc->Fields('total_jumps')?></option>
  <option value="151" <?php if($doc->Fields('total_jumps')=="151") echo "selected"; ?>>150 to 199 <?=$doc->Fields('total_jumps')?></option>
  <option value="201" <?php if($doc->Fields('total_jumps')=="201") echo "selected"; ?>>200 to 249 <?=$doc->Fields('total_jumps')?></option>
  <option value="251" <?php if($doc->Fields('total_jumps')=="251") echo "selected"; ?> >250 to 299 <?=$doc->Fields('total_jumps')?></option>
  <option value="300" <?php if($doc->Fields('total_jumps')== "300") echo "selected"; ?>>> 299 <?=$doc->Fields('total_jumps')?></option>
  </select>
  </td>
    <td align="left" valign="top">*ND</td>
  </tr>

 

try that and see if it is returning the expected values.

 

you also have an extra > in the last option.

Interesting results.  So, the expected value is being echoed.  But the correct drop down option is not being selected.  So for example, when I select a record with the value 151, the drop down box shows the default value as "Not Applicable. 151"

 

 

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.