dongtui6347 2017-02-14 05:47
浏览 52

将数据从ajax调用保存到codeigniter中的视图中的变量

I am getting the json data that i am sending to display in a div by append but i want to save that data to a variable so that i can use it even after page reload

here is my code

    jQuery('#npost').submit(function(e){

            e.preventDefault();
            var formData = new FormData(this);

            var url= '<?php echo base_url("user/postData"); ?>';

            jQuery.ajax({

                type: "POST",
                url:url,
                data: formData,
                dataType: 'json',
                cache: false,
                contentType: false,
                processData: false,
                success: function(data)
                {
                  var ParsedObject = JSON.stringify(data);

                 var json = $.parseJSON(ParsedObject);
                  alert(json.status);
                var value= json;

                 json.textdata;
                 //$("#status_data").val(json.textdata);

                 $('#result_table').append(json.textdata);



          },
                error: function(data){
                //error function
              }
           });            
        });

here is my controller

public function postData()
  {
    $post = $this->input->post();
    //print_r($post);
    unset($post['submit']);
    $this->load->model('Pmodel');
    $post_data=$this->Pmodel->post_data($post);

   echo json_encode($post);


  }

view

<div id='result_table' style="color:white;">
          <?php echo $status;  ?><br>
        </div>

can i save the data in a html element and echo it in the controller or is it possible to save it in the view ?

  • 写回答

1条回答 默认 最新

  • dongyue7796 2017-02-14 06:08
    关注

    In your success function write below code

                var json= $.parseJSON( data);
                if( json.success !== undefined ) {
                $("#span_id").text(json.status);
                }
    

    In your view

        <div id='result_table' style="color:white;">
        <span id="span_id"></span><br>
        </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真