1

I have a PHP code, where I need to make some manipulations with JS, and I tried the following

<?php
   include './parse.service.php';
   echo putContent();

   $jsScript = "
   <script type='text/javascript'>
   const json = require('./transacitions.json');
   window.onload = modifyData;

   function modifyData() {
       document.getElementById('n_transactions').innerHTML = parseInt(document.getElementById('n_transactions').innerHTML, 10) + json.data.length;
       document.getElementById('total_received').getElementsByTagName('font')[0].getElementsByTagName('span')[0].innerHTML = `${this.totalReceived(convertToFloat(document.getElementById('total_received').getElementsByTagName('font')[0].getElementsByTagName('span')[0].innerHTML))} BTC`;
       document.getElementById('final_balance').getElementsByTagName('font')[0].getElementsByTagName('span')[0].innerHTML = `${this.finalBalance(convertToFloat(document.getElementById('final_balance').getElementsByTagName('font')[0].getElementsByTagName('span')[0].innerHTML))} BTC`;
   }

   function convertToFloat(element) {
       var numb = element.match(/[+-]?\d+(\.\d+)?/g);
       numb = numb.join(\"\");
       return (parseFloat(numb, 10));
   }

   function totalReceived(quantity) {
       json.data.forEach(element => {
           if (element.finalSum > 0) {
               quantity += element.finalSum;
           };
       });
       return quantity;
   };


   function finalBalance(quantity) {
       json.data.forEach(element => {
           quantity += element.finalSum;
       });
       return quantity;
   };
   </script>";
   echo $jsScript;
?>

And when I echo the created "script", i get the message similar to this Uncaught Error: Call to undefined function totalReceived() how shall I modify the code, in sucha a way that JS will integrate normally in my PHP script.

3
  • 1
    What line does that error message refer to? Probably this.totalReceived? Why this? None of this is an object method, so no this needed. Commented Jul 30, 2018 at 10:06
  • Please add full code and exact where error is generated Commented Jul 30, 2018 at 10:07
  • I advice you to create a js file and include that file. That being said the problem probably arrives because you need to escape charaters Commented Jul 30, 2018 at 10:07

1 Answer 1

6

$ has special meaning inside PHP strings delimited with " characters, so ${this.totalReceived is causing the PHP engine to try to find an execute a function called totalReceived.

There's no apparent reason to use a PHP string here anyway. Just exit PHP mode and just output the code directly.

<?php
    include './parse.service.php';
    echo putContent();
?>
    <script type='text/javascript'>
    const json = require('./transacitions.json');
    window.onload = modifyData;

    // etc etc 
    </script>

Better yet. Move the JS to a separate file and include it with <script src>.

Sign up to request clarification or add additional context in comments.

1 Comment

I thought about the idea with <script src> but the problem is that I have there a <base> tag too, which messes my src...

Your Answer

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.