1

I am trying to pass values in variable of PHP retrieved from .csv file to Javascript variable. But the variable of javascript is get null values. Can any one help me to get value in java script variable. php tags

<?php
 $i=0;
 $handle = fopen("./uploads/csv_temp.csv", "r");
 while (!feof($handle) ) {
 $line_of_text = fgetcsv($handle, 1024, ",");
 $json['json_'] = $line_of_text[0];
 $a= $json['json_'];
 // $a= $line_of_text[0];
 // echo $json['json_'];
 $json = json_encode ( $a, JSON_FORCE_OBJECT );
 echo $a;
 }
?>
<html>
<head></head>
<body>
 <script>
 var abc= " <?php echo $a; ?> ";
 </script>
</body>
</html>
asked Apr 6, 2014 at 12:01

1 Answer 1

2

It looks like you need to call json_encode on $json['json'] before pass it to $a, You actually encode it AFTER you have passed the array to $a

<?php
 $i=0;
 $handle = fopen("./uploads/csv_temp.csv", "r");
 while (!feof($handle) ) {
 $line_of_text = fgetcsv($handle, 1024, ",");
 $json['json_'] = $line_of_text[0];
 **$a= json_encode($json['json_']);**
 echo $a;
 }
?>
<html>
<head></head>
<body>
 <script>
 var abc= " <?php echo $a; ?> ";
 </script>
</body>
</html>
answered Apr 6, 2014 at 12:09
Sign up to request clarification or add additional context in comments.

1 Comment

I've removed the line you requested

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.