gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 05:36:58 25 4
gpt4 key购买 nike

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>

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这是我的 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);
}
});

});

这里的想法是我想获取我在 Controller 中运行的查询的数据..并且该数据(例如“GuarantorFirstName”)将传递到 View 中找到的名字字段。

最佳答案

从 Controller 返回一个 json 响应,而不是 var_dump 数据,然后您可以轻松地从 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);
}
});

关于javascript - 如何使用 AJAX 将查询的值传递到输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40689194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com