man99
man99

Reputation: 47

Passing value from controller to view using codeIgniter dynamic template

how to parsing value from controller to view using CodeIgniter dynamic template like this? I want to show the value after doing insert, but always got Undefined variable error in view page when I call the title and category. This is my controller and view.

Controller (AdminController.php)

class AdminController extends CI_Controller
{
    public function book_controller($param1 = '')
    {
        $page_data['page_name']       = 'book';
        $page_data['page_title']      = 'Book';
        $page_data['page_function']   = __FUNCTION__;
        $page_data['page_breadcrumb'] = generate_breadcrumb(array('Book'));

        if ($param1 == 'addBook') {
            
            $data['title']     = $this->input->post('title');
            $data['category']  = $this->input->post('category');

            $this->md_book->addBook($data);

            $this->load->view('book', $data);

        } else if ($param1 == 'updateBook') {
            //
        } else if ($param1 == 'deleteBook') {
            //
        } 

        $this->load->view('index', $page_data);
    }
}

View (book.php)

<script type="text/javascript">
    function add_function() {
        swal({
                title: "Save Book ?",
                type: "warning",
                showCancelButton: true,
                showLoaderOnConfirm: true,
                confirmButtonText: "Ya",
                closeOnConfirm: false
            },
            function() {
                $.ajax({
                    method: 'POST',
                    url: '<?php echo site_url('admincontroller/book_controller/addBook') ?>',
                    data: $('#add-form').serialize(),
                    dataType: 'json',
                    success: function(resp) {
                        if (resp['status'] == 'success') {
                            $("#add-form")[0].reset();
                        }
                        return swal({
                            html: true,
                            timer: 1300,
                            showConfirmButton: false,
                            title: resp['msg'],
                            type: resp['status'],

                        });
                    }
                });
            });
    }
</script>

<div class="body">
    <?php echo form_open('admincontroller', array('id' => 'add-form', 'autocomplete' => "off")); ?>
    /** form add book */
    <?php echo form_close(); ?>

    <h2>Book Added! </h2>
    <?php echo $title ?>
    <?php echo $category ?>
</div>

$page_data is variable for the template, and $data is variable for my input. The insert running well but I don't have idea how to pass the $data from parameter addBook to the view. Can someone help me? Thank you.

Upvotes: 0

Views: 53

Answers (1)

Gayan Akmeemana
Gayan Akmeemana

Reputation: 41

Option 1 In the controller

$data['title']     = $this->input->post('title')?$this->input->post('title'):'NA';
$data['category']  = $this->input->post('category')?$this->input->post('category'):'NA';

Option 2 In the View

<?php echo (isset($title) && $title !='')?$title:'NA'; ?>
<?php echo (isset($category) && $category !='')?$category:'NA'; ?>

Upvotes: 0

Related Questions