如何使用AJAX

时间:2016-11-19 04:25:01

标签: javascript php jquery ajax codeigniter

im关于使用AJAX的新手。即时通讯使用Codeigniter 我的问题是如何获取查询的值并将其放在输入字段上。我创造了一些,但我能做的就是提醒他们。

模型

public function getguarantordata($id,$gid){ 
   $array = array('ClientID' => $id, 'GuarantorID' => $gid);
        $query =  $this->db->where($array)->get('tblguarantor');          
        return $query->result();
    }

查看

<h3>Guarantor Information</h3>
    <div>
     <input type="text" id="clientID" class="hide" value="<?php echo $this->session->loginid; ?>" > //temporary im using this to get the ID of the Client
   </div>
    <div class="form-group row ">
    <label for="" class="col-md-1">Previous Guarantor</label>
    <div class="col-md-3">
     <select class  = "form-control" id = "selectedvalue">   
      <option value="default">Default</option>
     <?php foreach ($guarantordata as $row): ?>      
     <?php echo '<option value = "'. $row->GuarantorID.'">' .$row->GuarantorFirstName. '</option>' ?>    //getting the ID of The Guarantor  
        <?php endforeach ?>
        </select>
     </div>
     <div class="col-md-3 btn btn-success" id="selectedG">Choose as Guarantor </div>
    </div>

    <div class="form-group row">
     <label for="" class="col-md-1 col-form-label">First Name</label>
     <div class="col-md-3"><input type="text" class="form-control" value =""></div>

     <label for="" class="col-md-1 col-form-label">Middle Name</label>
     <div class="col-md-3"><input type="text" class="form-control" value =""></div>

控制器

 function Getgdata($id,$gid){
   $guarantordata = $this->Model_user->getguarantordata($id,$gid);
  foreach ($guarantordata as $row){

  $data1 = array(

'GuarantorFirstName' => $row->GuarantorFirstName,
'GuarantorMiddleName' => $row->GuarantorMiddleName,



  );
 }

  echo var_dump($guarantordata); //i just want to view the result.
 }

的Javascript 这是我的javascript代码。

  var home = "http://localhost/";
  $('#selectedG').on('click', function(e){  
var test = $('#selectedvalue').val();
var cid = $('#clientID').val();

           e.preventDefault();          
                $.ajax({  
                     url: home + "Getgdata/" + cid+"/" + test,                         
                     method:"POST",  
                     data:this,  
                     contentType: false,  
                     cache: false,  
                     processData:false,  
                     success:function(data)  
                     {  

                         alert(data);
                     }  
                });  

      }); 

这里的想法是我想获取我在控制器中运行的查询数据..以及该数据,例如&#34; GuarantorFirstName&#34;将通过查看中的名字字段传递。

1 个答案:

答案 0 :(得分:1)

而不是var_dump数据,从控制器返回json响应,然后您可以轻松地从js访问值。

function Getgdata($id,$gid){
  $guarantordata = $this->Model_user->getguarantordata($id,$gid);
  foreach ($guarantordata as $row){

      $data1 = array(
              'GuarantorFirstName' => $row->GuarantorFirstName,
              'GuarantorMiddleName' => $row->GuarantorMiddleName);
      }
      // return json response 
      header('Content-Type: application/json');
      echo json_encode($data1);
 }

现在从ajax回复你可以轻松访问它们

 $.ajax({  

     url: home + "Getgdata/" + cid+"/" + test,                         
     method:"POST",  
     dataType: 'json', //set data type to json
     data:this,  
     contentType: false,  
     cache: false,  
     processData:false,  
     success:function(data)  {  
         alert(data.GuarantorFirstName);
         alert(data.GuarantorMiddleName);
     }  
});