Reputation: 243
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>
Upvotes: 1
Views: 199
Reputation: 6036
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>
Upvotes: 2