【问题标题】:fill form fields from database via ajax通过ajax从数据库中填写表单字段
【发布时间】: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


【解决方案1】:

在服务器端返回这样的数据而不是 print_r($posts_form3);

 echo json_encode($posts_form3);

像这样访问客户端的数据

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

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

像这样访问数据

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

【讨论】:

  • 我试过这个,它返回一个数组。但是当我访问字段名称时,它会显示“未定义”。我试过每一列总是说未定义。这是 js 行 var getJson = JSON.parse(data12);警报(getJson.name);
  • 你的访问错误使用了这个 alert(getJson[0].name);试试这个@tharaka
  • 很高兴帮助你@tharaka
【解决方案2】:

您需要传递 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";
         }
    }

【讨论】:

    【解决方案3】:

    而不是print_r($posts_form3); 使用:

    echo json_encode($posts_form3); 
    

    来自get_data_form3(){ 函数并在 ajax 中接收它,例如:

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

    您可以访问数据中的所有元素,例如:

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

    【讨论】:

    • 我试过这个,它返回一个数组。但是当我访问字段名称时,它会显示“未定义”。我试过每一列总是说未定义。这是 js 行 var getJson = JSON.parse(data12);警报(getJson.name);
    • 它是一种二维数组,所以请检查更新的答案
    猜你喜欢
    • 2014-08-22
    • 2012-02-05
    • 2013-09-04
    • 2014-09-02
    • 1970-01-01
    • 2011-02-25
    • 2010-12-05
    • 1970-01-01
    • 2020-11-05
    相关资源
    最近更新 更多