Home > Enterprise >  Pass PHP array integer to JavaScript function as a parameter?
Pass PHP array integer to JavaScript function as a parameter?

Time:10-11

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
        ))->result_array();
        
        $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"  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?

CodePudding user response:

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));
  • Related