简体   繁体   中英

Drop down select box with php

I wrote some php to deliver a select box. For some reason it refuses to select the correct option. When I try the HTML directly it works. To check I wasn't doing anything stupid. I copied the generated code and they put the code to generate and the html.

<dd>
  <select id="jform_my_foreign_key" class="inputbox " name="jform[my_foreign_key]" size="1">
   <option value="">- Select Order title -</option>
   <option value="13">00000013</option>
   <option value="12">00000012</option>
   <option value="9" selected="selected">00000009</option>
   <option value="8">00000008</option>
   <option value="7">00000001</option>
</select>
  </dd>

<select id="jform_my_foreign_key" class="inputbox " size="1" name="jform[my_foreign_key]">
   <option value="">- Select Order title -</option>
   <option value="13">00000013</option>
   <option value="12">00000012</option>
   <option selected="selected" value="9">00000009</option>
   <option value="8">00000008</option>
   <option value="7">00000001</option>
</select>

The top is generated by the php code and the bottom is me putting the html directly into the page

Code used is:

<dd><select id="jform_my_foreign_key" class="inputbox " size="1" name="jform[my_foreign_key]">
  <option value="">- Select Order title -</option>

<?php
                for($x=0;$x<count($ordersAvailableHoldingArray);$x+=2){
  if($ordersUseHoldingArray[0] ==$ordersAvailableHoldingArray[$x+1]){
$selected ="selected='selected'";
  }else{
$selected ="";
}
echo '<option '.$selected.' value="'.$ordersAvailableHoldingArray[$x].'">'.$ordersAvailableHoldingArray[$x+1].'</option>';
}
?>

  </select></dd>

I can't figure out why it doesn't work it looks right to me. Any help where to start debugging would be great

You might be better off using a foreach to put key and value in select as example below.

<form action="" method="post">

<select id="jform_my_foreign_key" class="inputbox" name="jform[my_foreign_key]"  size="1">
<option value="">- Select Order title -</option>
            <?php 

            $data_for_select = array(13=>'00000013',12=>'00000012',9=>'0000009');

            foreach($data_for_select as $k=>$v)
            { 
            if($k==$_POST['jform']['my_foreign_key'])
            {
                echo "<option value=\"$k\" selected=\"selected\">$v</option>";
                }
            else
            {
                echo "<option value=\"$k\">$v</option>";
                } 
            } 
            ?>
            </select>

<input name="jello" type="submit" value="send">
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM