简体   繁体   English

如何使用AJAX将查询的值传递给输入字段

[英]How to Pass Value of my Query to input fields using AJAX

im Very new on using AJAX. im在使用AJAX方面非常新。 im using Codeigniter my question is how can i get the value of my query and put it on the input fields. 即时通讯使用Codeigniter我的问题是如何获取查询的值并将其放在输入字段中。 i have created some but all i can do is to alert them. 我创建了一些,但是我能做的就是提醒他们。

Model 模型

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

View 视图

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

Controller 控制者

 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 this is my javascript code. 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);
                     }  
                });  

      }); 

the idea here is i want to get the data of my query which i run in controller.. and that data for example "GuarantorFirstName" will be pass on Firstname field found in View . 这里的想法是我要获取我在控制器中运行的查询的数据。例如,“ GuarantorFirstName”等数据将传递给View中的“名字”字段。

instead of var_dump data, return a json response from controller then you can access values easily from js. 代替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);
 }

now from ajax responce you can access them easily 现在,从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);
     }  
});   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在jQuery ajax中传递多个输入字段的值? - Pass value of multiple input fields in jQuery ajax? 如何使用Ajax和PHP将复选框值传递给文本输入? - how to pass checkbox value to text input using Ajax and PHP? 如何使用AJAX获取输入字段的值并将其传递给PHP字符串? - How To Get The Value of an input field using AJAX and Pass it To A PHP String? 如何使用 AJAX 中的查询字符串将数据/值传递给 controller - How to pass data/value to controller by using query string in AJAX 使用AJAX将输入值传递给控制器​​的困难 - Difficulties using AJAX to pass input value to controller 如何获取输入字段值的数组并将其通过Ajax调用传递 - How to take an array of input fields values and pass it through an Ajax call 根据使用 ajax 在下拉列表中选择的内容填充我的输入字段 - Populating my input fields depending on what selected in dropdown using ajax 如何将输入值传递给 Cypher 查询? - How to Pass Input Value into Cypher Query? 如何使用AJAX将表单选择输入的值传递给php文件而不提交? - How to pass the value of a form select input to a php file using AJAX without submitting it? 如何将具有相同类和名称的多个输入字段传递给ajax请求并根据回复更改相应的字段? - How to pass multiple input fields having same class and name to ajax request and change corresponding fields based on reply?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM