Pass PHP array integer to JavaScript function as a parameter?


i am using PHP Codeigniter and trying to pass array from Controller to JavaScript function using onClick() but could not find any solution.

Here is my code in controller:

function get_class_students_mass_pdf($class_id) {

        $students = $this->db->get_where('enroll', array(
            'class_id' => $class_id, 'year' => $this->db->get_where('settings', array('type' => 'running_year'))->row()->description
        $invoiceid = array();
        foreach ($students as $row) {
            $invoice_id =  $this->db->get_where('invoice', array('student_id' => $row['student_id']))->row()->invoice_id;

                $invoiceid[] = $invoice_id;

            $invoiceid = json_encode($invoiceid);
        echo '<br><br><button style="margin-left: 36px;" type="button" class="btn btn-default" onClick="invoice_view_modal('.$invoiceid.')"> ' . get_phrase('generate_pdf') . ' </button>';

My JavaScript Function in View:

    function invoice_view_modal(invoiceid) {
          showAjaxModal('<?php echo site_url('modal/popup/modal_view_mass_invoice/');? 
          >' + invoiceid);

$invoiceid is my array that I am trying to access in my JavaScript Function using onClick() method.How do I access it?


I have same problem in my project and after debugging i came to know that javaScript function not accepting the special characters in the array like ",". so i simply use htmlspecialchars() and it works for mine.
Try this,

$invoiceid = htmlspecialchars(json_encode($invoiceid));

Answered By – Saqib Ali

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published