0

I have the following code and it insert the necessary data to the database perfectly. but the problem is that when i want to implement select="select".. so that if the customer return he will see what he choose from the dropdown menu...how can i do that, any idea...

<pre>
<?php
//array generated from mysql to feed first dropdown menu.
$newOptions = array();
    foreach ($input as $option) {
        $wclID = $option['desc'];
        $nameF = $option['fame'];
        $nameL = $option['lname'];
        $id = $option['ID'];
        $newOptions[$wclID][$id] = $nameL." ".$nameF;
    }

//second array for second dropdown menu
$array = array('ent','res');    
?>


   <div class="control-group">
        <div class="controls">
            <form  id="checkEnt" name="che" method="post" action="com.php?ID=<?=$ID?>">
                <input type="hidden" value="che" name="ent">
                <table class="table table-striped span10">
                    <tbody>
                        <? foreach ($newOptions as $wclID => $list) {  ?>
                            <tr><td width="5%">
                                    <h5><?=$wclID?> AA</h5>
                                </td>
                                <td width="10%">
                                    <label class="control-label" for="inputWei"><?=_('Boy')?></label>  
                                    <select class="input-xlarge" id="input" name="drop[0][]">   
                                        <option value=""><?=_('[select]')?></option>
                                        <?php 
                                            foreach ($list as $key => $value) {
                                            ?><option value="<?=$key?>"><?=$value?></option>

                                            <?php    } ?>
                                    </select>
                                </td>
                                <td width="10%">
                                    <label class="control-label" for="inputWei"><?=_('Res')?></label>
                                    <select class="input-xlarge" id="drop" name="drop[1][]">
                                        <option value=""><?=_('[select]')?></option>
                                        <?php
                                         foreach ($array as $key => $value) {
                                            ?><option value="<?=$value?>"><?=$value?></option>

                                            <?php } ?>
                                    </select>
                                </td>
                            </tr>

                              <?
                            }
                        ?> 

                            <tr><td colspan="3">
                                    <div class="modal-footer">
                                        <button type=reset class="btn btn-danger"><?=_('Reset')?></button>
                                        <button class="btn btn-primary" ID="btnSave"><?=_('Save')?></button>
                                    </div>
                                </td>
                            </tr> 


                    </tbody>
                </table> 
            </form>   
        </div>
    </div>
</pre>
5
  • so there actually is no error? Commented Jul 23, 2013 at 15:33
  • What is going on with your form action? Your PHP declaration is part of the action statement. Commented Jul 23, 2013 at 15:35
  • ohhh, sorry for saying error...question..how could i implement the select="selected" attribute...on this code? Commented Jul 23, 2013 at 15:36
  • what the action does is send the data to mysql table and properly saved. i use pdo function to achieve this... Commented Jul 23, 2013 at 15:37
  • possible duplicate of stackoverflow.com/questions/3747212/… Commented Jul 23, 2013 at 15:38

2 Answers 2

2

I recently did something similar to this. This is for data that is coming back from the database correct? If so then I would implement something similar to this.

On the option tags I would try an if statement to see what is in the database and then if it is call the selected=selected

<option value="Something" <? if(in_array('Something',$nameofArray)) echo 'selected="selected"';?>>Something</option>

or in your case you might want to try

<?foreach ($list as $key => $value) {?>
    <option value="$value" <? if($value) echo 'selected="selected"';?>>$value</option>

 <?}?>
Sign up to request clarification or add additional context in comments.

7 Comments

which array we are taking about, in_array('Something',$nameofArray) could u please elaborate
That was just an example. this in_array is where your information coming from the database should be. So in your case it would just be $value so if your value is there then it will be selected. Make sense?
$html_form .= '<option value="'.$value.'"'. if($value) echo 'selected="selected">'.$value.'</option>'; why i get error
hi zazvorniki, could you please see the above code and suggest please how could i insert selected="selected" attribute..Please?
i see this code....of yours..<?foreach ($list as $key => $value) {?> <option value="$value" <? if($value) echo 'selected="selected"';?>>$value</option> <?}?>
|
-1

Use the "selected" attribute of the option tag:

http://www.w3schools.com/tags/att_option_selected.asp

1 Comment

but how? i just put like this <option value="<?=$key?>" select="selected"><?=$value?></option>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.