2017-10-20 174 views
0

我想从下拉列表中选择一个合同,但它看起来像Ajax调用返回所有合同的信息,而不仅仅是发送的id。请记住,我对ZF2非常陌生。ZF2 Ajax调用返回所有信息,而不仅仅是发送的ID

// 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 任何想法?

+0

'selectLtContr acts($ id);' - 你在这里接受一个$ id变量......但$ id没有在getIdAction()方法的任何地方定义。我不知道你的具体框架,但看起来非常明显,你需要定义变量,然后才能使用它。应该是'getIdAction($ id)',也许?检查您的框架的文档,了解如何访问客户端发送的输入变量。 – ADyson

+0

很明显,您不会在'selectLtContracts'方法的任何地方使用'$ id' - 因此即使您将实际值传递给此方法,您也不会使用它来限制数据库的结果。因此,为什么它会返回一个大列表而不是单个项目。 – ADyson

+0

这是正确的。谢谢。答案可能在ContractTable.php模型中。我今晚会弄清楚。 – Blackcoat77

回答

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); 

...

相关问题