简体   繁体   中英

'unexpected token: identifier error' - assigning php json array type variable value to a javascript function from php code

I am trying to call JavaScript function in php and pass it value of a php json array type variable as an argument. I found from search on SO forum one way to do this is to echo/print_r the variable value to a js var inside a js script within php code. I am trying do it this way but I am not able to recover from 'unexpected token: identifier error ' while doing so.

I am trying to figure out the reason of syntax error but couldn't. I tried different ways what I found; by putting quotes single/double around php part within the script, without quotes as some places I found solution with quotes some places without but no one seems working.

Here is my code. It will be very helpful if someone sees it and point what is causing this error.

    <script>
    dspChrt(WData);
    .......
    </script>
<HTML>
     <?php

          $WData;
          require("Connection.php");

                try {
                    $stmt = $conn->prepare("Select humidity, temperature FROM weatherdata");
                            $stmt->execute();

                    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
                    foreach($stmt->fetchAll() as $k=>$v) {       
                    $WData = json_encode($v);
                    //print_r($WData);
                    }?>

                    <script>
                    var Wdata = <?php print_r($WData);?>
                    dspChrt(WData);
                    consol.log(WData);
                    </script>

                    <?php
                        }
                    catch(PDOException $e) {
                    echo "Error: " . $e->getMessage();
                    }

         ?>
</HTML>

You should encode your PHP into JSON to pass it to JavaScript.

And you should prepare your data first.

<?php
    $data = array('xxx'=>'yyy');
?>
<script>
    var data = <?php echo json_encode($data); ?>;
    //then in js, use the data
</script>

for your code, there are too many errors to be fixed:

  <HTML>
    <?php
    require("Connection.php");
    $stmt = $conn->prepare("Select humidity, temperature FROM weatherdata");
    $stmt->execute();

    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
    $WData = array();
    foreach($stmt->fetchAll() as $k=>$v) {       
      $WData[] = $v;
    }
    ?>

    <script>
      var WData = <?php echo json_encode($WData);?>;
      console.log(WData);
      dspChrt(WData);
    </script>
  </HTML>

First of all you need to parse the JSON using JSON.parse.

Also you need to change the sequence of php and javascript code.

If you want to assign php data to Javascript variable, please retrieve data using php first and write javascript code below it.

For example :

<?php

$v = array(1,2,3);
$data = json_encode($v);

?>

<script>

 var WData = JSON.parse('<?php echo $data; ?>');
 dspChrt(WData);

</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM