baladpi
baladpi

Reputation: 47

display json value in select tag(view) zend framework 2

my model: to fetch the data from database

public function getAllProjectDomain() {


        $dbadapter = $this->adapter;
        $sql = new Sql($dbadapter);
        $select = $sql->select();
        $select->columns(array('cmain'));
   $select->from('projcat');

    $selectString = $sql->getSqlStringForSqlObject($select);
        $result = $dbadapter->query($selectString, $dbadapter::QUERY_MODE_EXECUTE);
            foreach ($result as $row) {
             $entity = array(
            "domain"      => $row->cmain
        );
        $entities[] = $entity;
        }

        return $entities;   

    }

my controller:

 public function domainAction()
        {
       if ($this->getRequest()->isPost()) {

            $service = $this->getServiceLocator()->get('UserService');
            $result = $service->getAllProjectDomain();
            //it will go to service and the method from table

            $mail = $result;
        }
         $results = new JsonModel(array(
        'mail' => $mail,    
        ));

        return $results;
    }

my view:

<div class="form-group"> 
        <label for="experience" class="col-lg-2 control-label">Relevant Experience </label>
        <div class="col-lg-3">
            <select class="form-control" id="experience" name="experience" title="Please select experience">
                 <option value="" selected="selected" >--Select--</option>
         <?php  foreach ($mail as $value) { ?>
  <option value="<?php echo $value ?>"> <?php echo $value ?> </option>
<?php } ?>

            </select>
        </div>
    </div>

my jquery:

$(document).ready(function() {

$.post("<?php echo $this->url('checkcourse'); ?>", {},
    function(data) {

        if (data.mail)
        {
            document.getElementById('experience').value=data.mail;
        }
        else
        {
             alert('no data');
    }     
    }, "json");
});

Here i am trying to display the drop down option. The list of option should come from the datbase. Now i am getting the json value in jquery(view file), but i dont know how to make it to display in select tag.

Upvotes: 0

Views: 855

Answers (1)

dixromos98
dixromos98

Reputation: 754

edit the view:

> <div class="form-group"> 
    >         <label for="experience" class="col-lg-2 control-label">Relevant Experience </label>
    >         <div class="col-lg-3">
    >             <select class="form-control" id="experience" name="experience" title="Please select experience">
    >                  
    > 
    >             </select>
    >         </div>
    >     </div>

in the jquery function add this:

$('<option value="">'+'Select Action...'+'</option>').appendTo('#experience');
               for (var i = 0; i < data.length; i++){

                   $('<option value="'+data[i]['mail']+'">'+data[i]['mail']+'</option>').appendTo('#experience');
                }

instead of a json model try to return a json array

$this->getResponse()->setContent(Json::encode($arrayOfData))

Upvotes: 0

Related Questions