Javascript 通过ajax从数据库中填充表单字段
我正试图通过ajax jquery从数据库中填充表单字段。 我使用Codeigniter。 我可以检索数据库值并将其作为此图像通知给数组。 我的问题是如何通过javascript访问这个php数组值? 我想使用js填充这个数组中的表单字段。 感谢您的帮助:) 这是我的JavascriptJavascript 通过ajax从数据库中填充表单字段,javascript,php,jquery,ajax,codeigniter,Javascript,Php,Jquery,Ajax,Codeigniter,我正试图通过ajax jquery从数据库中填充表单字段。 我使用Codeigniter。 我可以检索数据库值并将其作为此图像通知给数组。 我的问题是如何通过javascript访问这个php数组值? 我想使用js填充这个数组中的表单字段。 感谢您的帮助:) 这是我的Javascript $("#header3").on("click", ".edit_button", function(e) { var site_path = $('#sitePath').val();
$("#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";
}
}
在服务器端像这样返回数据,而不是打印($posts\u form3) 像这样访问客户端中的数据
success:function(data12)
{
var datas = JSON.parse(data12);
console.log(datas[0]['name']);
}
datas[0]['name']; datas[0]['address']; datas[0]['sex'];
像这样访问数据
success:function(data12)
{
var datas = JSON.parse(data12);
console.log(datas[0]['name']);
}
datas[0]['name']; datas[0]['address']; datas[0]['sex'];
而不是print\r($posts\u form3)代码>使用:
echo json_encode($posts_form3);
从get_data_form3(){
函数中获取数据并在ajax中接收,如:
success:function(data12)
{
var data = JSON.parse(data12);
}
您可以访问数据中的所有元素,如:
data[0]。column
或data[0][column]
您需要以JSON格式传递数据才能在ajax中获取数据。请用以下代码替换您的代码
$("#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)
{
var getData = JSON.parse(data12);
console.log(getData);
}
,
error:function (xhr, ajaxOptions, thrownError){
//On error, we alert user
alert(thrownError);
}
})
});
下面是php代码
public function get_data_form3(){
$insert_id = $this->input->post('myData');
$this->load->model('queries1');
if( $posts_form3= $this-> queries1 ->getPostsFor_form3i($insert_id) ) {
echo json_encode($posts_form3);
}else{
echo "failed";
}
}
返回类似以下内容的数据:echo json_encode($posts_form3);您可能希望在php端使用echo json_encode($posts_form3);
,在javascript端使用var getJson=json.parse(data12);
。我尝试了这个方法,它返回了一个数组。但是当我访问字段名时,它会显示“undefined”。我尝试过每一列总是说未定义。这是js行var getJson=JSON.parse(data12);alert(getJson.name);我尝试过这个,它返回一个数组。但是当我访问字段名时,它说“未定义”。我尝试过每一列总是说未定义。这是js行var getJson=JSON.parse(data12);alert(getJson.name);这是一种2d类型的数组,因此请检查更新的应答。我尝试了此操作,它返回了一个数组。但当我访问字段名称时,它会显示“未定义”。我尝试过的每一列都会显示“未定义”。下面是js行var getJson=JSON.parse(data12);alert(getJson.name);您的访问错误用法,如此alert(getJson[0].name);试试这个@tharakaglad来帮助你@tharaka