简体   繁体   English

通过ajax从数据库填写表单字段

[英]fill form fields from database via ajax

I'm trying to fill form fields from database via ajax jquery. 我正在尝试通过ajax jquery从数据库填充表单字段。 I use Codeigniter. 我使用Codeigniter。 i can retrieve and alert database values to an array as this image. 我可以检索数据库值并将其作为此图像提醒数组。 alert of the data array my problem is how to access this php array values via javascript? 数据数组的警报我的问题是如何通过JavaScript访问这个PHP数组值? i want to fill form fields from this array using js. 我想用js从这个数组填充表单字段。 appreciate help :) 感谢帮助:)

here is my Javascript 这是我的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);
            }
        })
    });

here my controller code 这是我的控制器代码

 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";
         }
    }

Return the data like this on server side instead of print_r($posts_form3); 在服务器端返回这样的数据而不是print_r($ posts_form3);

 echo json_encode($posts_form3);

Access the data in client side like this 像这样访问客户端的数据

 success:function(data12)
        {
         var datas = JSON.parse(data12);

          console.log(datas[0]['name']);
        }

Access the data like this 像这样访问数据

  datas[0]['name']; datas[0]['address']; datas[0]['sex'];

Instead of print_r($posts_form3); 而不是print_r($posts_form3); use: 采用:

echo json_encode($posts_form3); 

from the get_data_form3(){ function and receive it in ajax like: 来自get_data_form3(){ function并在ajax中接收它:

success:function(data12)
{
    var data = JSON.parse(data12);
}

You can access all the element in data like: 您可以访问数据中的所有元素,例如:

data[0].column or data[0][column] data[0].columndata[0][column]

You need to pass data in JSON format for get data in ajax. 您需要以JSON格式传递数据以获取ajax中的数据。 Please replace you code with below code. 请用以下代码替换您的代码。

$("#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);
            }
        })
    });

And below php code. 并在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";
         }
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM