【发布时间】:2017-10-21 15:34:26
【问题描述】:
我正在尝试通过 ajax jquery 从数据库中填充表单字段。 我使用 Codeigniter。 我可以检索数据库值并将其警报到数组作为此图像。 alert of the data array 我的问题是如何通过 javascript 访问这个 php 数组值? 我想使用 js 从这个数组中填充表单字段。 感谢帮助:)
这是我的 Javascript
$("#header3").on("click", ".edit_button", function(e) {
var site_path = $('#sitePath').val();
e.preventDefault();
var clickedID = this.id.split('_'); //Split ID string
var DbNumberID = clickedID[1]; //and get number from array
var myData = DbNumberID; //build a post data structure
//getting data for the form from accuse table
$.ajax({
url:site_path +'/queries/get_data_form3',
method:'POST',
data:{myData:myData},
success:function(data12)
{
alert(data12);
}
,
error:function (xhr, ajaxOptions, thrownError){
//On error, we alert user
alert(thrownError);
}
})
});
这里是我的控制器代码
public function get_data_form3(){
$insert_id = $this->input->post('myData');
$this->load->model('queries1');
if( $posts_form3= $this-> queries1 ->getPostsFor_form3i($insert_id) ) {
print_r($posts_form3);
}else{
echo "failed";
}
}
【问题讨论】:
-
像这样返回数据 echo json_encode($posts_form3);
-
你可能想在php端使用
echo json_encode($posts_form3);,在javascript端使用var getJson = JSON.parse(data12);。 -
我试过这个,它返回一个数组。但是当我访问字段名称时,它会显示“未定义”。我试过每一列总是说未定义。这是 js 行 var getJson = JSON.parse(data12);警报(getJson.name);
标签: javascript php jquery ajax codeigniter