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
shrestha
2431 gold badge4 silver badges13 bronze badges
1 Answer 1
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
Garbit
6,0946 gold badges43 silver badges78 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Garbit
I've removed the line you requested
default