-1

have been trying to pass a PHP array to a JavaScript array, but when I read out the content of that JavaScript array it shows literally [object, object].

if($error!='') {
    $this->output
        ->set_content_type('application/json')
        ->set_output(json_encode(array(
            'message' => array(
                array(
                    'status'=>'error',
                    'message'=> $error1
                ),
                array(
                    'status'=>'error',
                    'message'=> $error2
                )
            )
        )));
}

$error1='Size Title Required !';
$error2='Size Code Required !';

I have been trying to pass a PHP array to a JavaScript array, but when I read out the content of that JavaScript array it shows literally [object, object].

JO3-W3B-D3V
  • 2,124
  • 11
  • 30

3 Answers3

0

do this to php array

var js_obj = <?php echo json_encode($phpArray); ?>)
 console.log(js_obj); // to check in console

and use it in javascript then

Ahmed Sunny
  • 2,160
  • 1
  • 21
  • 27
-1

That's because you have an JSON object. Use JSON.Stringify(YourObject)

WKoppel
  • 504
  • 3
  • 15
  • i'm new at this, can you please tell me how to do this? – Samir Raihan Jan 30 '19 at 11:28
  • show your JavaScript first where you are trying to access the variables. – WKoppel Jan 30 '19 at 11:30
  • $.ajax( { url: "", type : "POST", dataType : "JSON", data : data, processData : false, contentType : false, cache : false, async : false, success: function (data) { if(data.status=='error'){ $.toast({ heading: 'Error', text: data.message, position: 'bottom-right', loaderBg:'#ff6849', icon: 'error', hideAfter: 3000 }); } }, – Samir Raihan Jan 30 '19 at 11:34
-1

try using json_encode() method in PHP before passing to javascript.

And Use JSON.parse() method in JavaScript to fetch