ZF2 Ajax调用返回所有信息,而不仅仅是发送的id

时间:2017-10-20 13:54:58

标签: php ajax zend-framework2

我试图从下拉列表中选择一个合同,但看起来Ajax调用正在返回所有合同的信息,而不仅仅是正在发送的ID。请记住,我对ZF2很新。

// view.phtml

<script>
function loadContractId(id)
 {
    $.getJSON('<?php echo $this->basePath();?>/ajax/getId/'+id+'', function(data) {


    $("#ctrid").text(data["arr"][0].ctr_id);
    $("#ctrspec").text(data["arr"].ctr_spec);
    $("#ctrnutype").text(data["arr"].ctr_nu_type);
    $("#ctrlocationcity").text(data["arr"].ctr_location_c);
    $("#ctrlocationstate").text(data["arr"].ctr_location_s);

   $.each(data["arr"], function (index, item) {
      console.log(item);
   });

    });
    $("#contact_holder").css('visibility','visible');
 }

</script>


<div id="loc_placement_panel" class="p0pup">
  <form name="loc_placement_form" method="post" action="<?php echo $this->basePath(); ?>/booking/view/add">
      <input type="hidden" name="ph_id" value="<?php echo $ar["ph_id"]; ?>">
      <input type="hidden" name="pres_status" value="2">
      <input type="hidden" name="ph_name" value="<?php echo $ar["ctct_name"]; ?>">

      <!--<input type="hidden" name="addon" value="see below">-->
      <strong>Placements</strong><br/><br/>

      <label><strong>Contract:</strong></label>

      <select name="contactlist" id="contactlist" onchange="loadContractId($('#contactlist').val())">
        <?php
                foreach ($ctrLT as $row=>$ar_contracts)
                {
                    echo "<option value='".$ar_contracts['ctr_id']."'>";
                    echo $ar_contracts['ctr_no'];
                    echo "</option>";
                }
            ?>
        </select>

        <div id="contact_holder" style="visibility: hidden">
            <strong>Ctr id: </strong><span id="ctrid" ></span><br/>
            <strong>Spec: </strong><span id="ctrspec" ></span><br/>
            <strong>Nurse Type: </strong><span id="ctrnutype" ></span><br/>
            <strong>City: </strong><span id="ctrlocationcity" ></span><br/>
            <strong>State: </strong><span id="ctrlocationstate" ></span><br/>
        </div>


      <label><strong>User Name:</strong></label>
      <input type="text" name="loc_location" id="loc_location" value="<?php echo $ar["ctct_name"]; ?>"  />


      <label><strong>$ltcontracts:</strong></label>
      <textarea id="txtArea" rows="10" cols="100" name="loc_location" id="loc_location" value=""><?php '<pre>'; print_r($ltcontracts); '</pre>';?></textarea>


      <br/><br/>
      <!-- <input type="submit" value="Submit" id="loc_placement_submit_btn" name="loc_placement_submit_btn" /> -->
      <input type="button" value="Cancel" id="loc_placement_cancel_btn" />
  </form>
</div>

// AjaxController.php

 // LT contracts
    public function getId($id) {
        $id = (int) $id;
        return $this->getResortObject('retainedResort',$id);
    }

  // LT contracts
    public function getIdAction() {

        $result = new \stdClass();

        $arr = $this->getContractsTable()->selectLtContracts($id);
        $result->code = Response::STATUS_CODE_200;

        $result->arr = $arr;
        $json = Json::encode($result);
        $response = $this->getResponse(); //new Response();
        $response->setStatusCode($result->code);
        $response->getHeaders()->addHeaders(array('Content-Type'=>'application/json'));
        $response->setContent($json);
        return $response;
    }

// ContractTable.php

我也尝试使用选定的ID,($select->where('ctr_id = ?', $id);),但它没有用。

public function selectLtContracts($id = 0, array $ar = null) {


    $this->table='allcontracts';
    $select = new Select($this->table);
    $select->where->like('ctr_no', '%LT');
    $resultSet = $this->selectWith($select);

    $ar = array();
    if($resultSet)
    {
    $i=0;
      foreach ($resultSet as $row) {

        $ar[$i]['ctr_id']=$row->ctr_id;
                $ar[$i]['ctr_no']=$row->ctr_no;
                $ar[$i]['ctr_spec']=$row->ctr_spec;
                $ar[$i]['ctr_nu_type']=$row->ctr_nu_type;
                $ar[$i]['ctr_location_c']=$row->ctr_location_c;
                $ar[$i]['ctr_location_s']=$row->ctr_location_s;
                $ar[$i]['ctr_nurse']=$row->ctr_nurse;
                $ar[$i]['ctr_type']=$row->ctr_type;
                $ar[$i]['ctr_marketer']=$row->ctr_marketer;
                $ar[$i]['ctr_recruiter']=$row->ctr_recruiter;
        $i+=1;
      }
    }

    return $ar;
  }

当我从下拉列表中选择单个合同时,这是我从控制台获取的内容:

enter image description here 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

基本上我忘记在路由中添加getIdAction()'id'参数:

$id = (int) $this->params()->fromRoute('id', 0);

在ContractsTable.php中我需要添加equalTo() predicate

...

if($id!='' && $id > 0)
  $where->equalTo('ctr_id', $id);

  $select->where($where);

...