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>

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>
Sign up to request clarification or add additional context in comments.

1 Comment

I've removed the line you requested

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.