Return json object in codeigniter (flashdata)

How can I return a json object in this case (messages) in a view (admincp_index). The method below works fine, but I really would like to give it some animations.

Regards, Phil

/* AdmincontrolPanel */
function index()
{
    $data['messages'] = $this->session->flashdata('messages');
    $data['content'] = 'admincp/admincp_index';
    $this->load->view('backend/template', $data);
}


 function applicant()
 {
      $id = $this->input->post('id');

      if($this->input->post('accept'))
      {
            if($this->admincpModel->accept_applicant($id) == TRUE)
            {
                 $this->session->set_flashdata('messages', '<div class="ok">Applicant Added!</div>');
                 redirect('admincp');
            }            
 }

/* admincp_index */

if($messages){
    // echo messages
}
+3
source share
3 answers

There are three things to keep in mind:

  • The browser can cache the JSON response, so we recommend adding a timestamp to the end of the URL so that the data is fresh. (This is true for the GET method, not necessarily the case with POST, though).

  • The content type of the JSON response must be " application / json " or " text / javascript ".

  • json_encode PHP 5.2, , , , , .

PHP 5.1.6, - , , , . " JSON", JSON, .

, / jQuery JSON . .

:

// the jQuery POST URL includes a time stamp
var now = new Date();
$.ajax({
    type: "POST",
    url: "page/post/" + now.valueOf().toString(),
    data: {},
    dataType: "json",
    success: function (result) {
        alert(result.message);
    }
});

(/application/controllers/page.php):

class Page extends CI_Controller {

    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
    }

    function post($TimeStamp)
    {
        /* process request... $Timestamp may or may not be used here. */

        $data['json'] = '{"message":"The post was handled successfully."}';
        $this->load->view('json_view', $data);
    }
}

(/application/views/json_view.php):

<?php
$this->output->set_header('Content-Type: application/json; charset=utf-8');
echo $json;
?>
+12

json.

$this->output
    ->set_content_type('application/json')
    ->set_output(json_encode(array('foo' => 'bar')));
+23

You are doing the wrong way. If you want to get a json object, AJAX is the best way to handle this. In your view, admincp_index (with jquery)

$.ajax({
        type: 'POST',
        url: 'controller/applicant',
        data: 'your post data',
        success: function(response) {
            var response = $.evalJSON(r);
            if(response.message) {
               //do some animation
            }
        }
    });

applicant method

function applicant()
{
  $id = $this->input->post('id');
  if($this->input->post('accept'))
  {
        if($this->admincpModel->accept_applicant($id) == TRUE)
        {
             echo json_encode(array('message'=>'<div class="ok">Applicant Added!</div>'));
             exit();
        }         
   }
0
source

Source: https://habr.com/ru/post/1786636/


All Articles