JavaScript Cannot get value from PHP to Javascript cannot return value from iterator,cannot cast value from string

So in my PHP file I create a JSON Object which I need to get to my Javascript file for processing. I can't seem to transfer anything however, not even with a simple string like this:-

index.php

<?php
  $cookie= "blueberry cake";
?>;

script.js

   var details = "<?php echo $cookie; ?>";
   function myFunction() { 
        document.getElementById("demo").innerHTML = details;
   }

index.html

<html>

<head>
    <script src="script.js"></script>
    <title>antgus</title>
</head>
<body>

    <h1>Welcome to my page! </h1>

    <button type="button" onclick="myFunction()">Try it</button>

    <p id="demo">A Paragraph.</p>


</body>

I got a button in my HTML, so when I press it the function myFunction get's called. It's supposed to change a p element with id "demo" to the string "blueberry cake"

Any idea what I'm doing wrong here?

EDIT

I now have it working except one thing, I can't pass a JSON object to a function i Javascript. The console throws an error saying that the arguments is wrong, it thinks I am passing multiple strings. index.php

    <?php
    $test = "blue";
    $logLines = file('../../../home/shares/flower_hum/humid.log');
    $entries = array_map("clean",$logLines);
    $finalOutput = [
        'humid'   => $entries
    ];
    function clean($string){
        return json_decode(rtrim(trim($string),','),true);
    }

    $json = json_encode($finalOutput, JSON_UNESCAPED_SLASHES);
    echo $json; //displays valid JSON, I have checked it.
?>

<html>
    <head>
        <script src="script.js"></script>
        <title>antgus</title>
    </head>
    <body>
        <p>Welcome to my page! Down below you can see the data:</p>
        <button type="button" onclick='myFunction("<?php echo $json ?>")'>Try it</button>
        <p id="demo">A Paragraph.</p>
    </body>
</html>

script.js

function myFunction(jsonObject) {
    document.getElementById("demo").innerHTML = "INCOMING DATA:";
    alert(jsonObject);
}
    enter code here
Answer:1

The external js file will not be parsed as php meaning that you cannot access that variable. You can save your js as php (NOT RECOMMENDED). alternatively a)

include that function at the bottom of the bottom of the php page:

<?php
  $cookie= "blueberry cake";
?>;
    //js further down the same page
  <script>
    var details = "<?php echo $cookie; ?>";
   function myFunction() { 
        document.getElementById("demo").innerHTML = details;
    }
 </script>

or b) declare a global variable in the php page so that the external js can access it:

//php page
    <?php
      $cookie= "blueberry cake";
    ?>;
<script> var details = "<?php echo $cookie; ?>";</script>


//js file
       function myFunction() { 
          //details will be accessible since it has been declared in the global space of the index.php page
            document.getElementById("demo").innerHTML = details;
       }
Answer:2

Encode the json object using json_encode, that is

<script> var details = "<?php echo json_encode($cookie); ?>";</script>
Answer:3

Remove the quotes around php, dude. You are assigning string instead of the php variable value.

var details = "'" + <?php echo $cookie; ?> + "'";

This will assign the actual $cookie value instead of the string '<? php echo $cookie; ?>'.

Answer:4

For some reason when I echo inside the value="" of a hidden input tag. It for some reason has a max character size. Its much smaller then I need it to be as I need it to put content from my DB into ...

For some reason when I echo inside the value="" of a hidden input tag. It for some reason has a max character size. Its much smaller then I need it to be as I need it to put content from my DB into ...

  1. hidden input value jquery
  2. hidden input value javascript
  3. hidden input value html
  4. hidden input value change event jquery
  5. hidden input value change jquery
  6. hidden input value
  7. hidden input value change event
  8. hidden input value not setting
  9. hidden input value array
  10. hidden input value change
  11. hidden input value empty
  12. hidden input value php
  13. hidden input value angular
  14. hidden input value true
  15. hidden input value angularjs
  16. hidden input value get
  17. input hidden value boolean
  18. input hidden value js
  19. input hidden value undefined
  20. input hidden value php post

I am interested to hide resultcaptcha message as soon as the user ticks and verifies the google recaptcha. Only the first part (v.length== 0) works. The else if doesn't work. you can check the form ...

I am interested to hide resultcaptcha message as soon as the user ticks and verifies the google recaptcha. Only the first part (v.length== 0) works. The else if doesn't work. you can check the form ...

  1. google recaptcha validation failed
  2. google recaptcha validation example
  3. google recaptcha validation php
  4. google recaptcha validation jquery
  5. google recaptcha validation not working
  6. google recaptcha validation javascript
  7. google recaptcha validation laravel
  8. google recaptcha validation message
  9. google recaptcha validation before submit
  10. google recaptcha validation in codeigniter
  11. google recaptcha validation
  12. google recaptcha validation example php
  13. google recaptcha validation on client side
  14. google recaptcha validation asp.net
  15. google recaptcha validation c#
  16. google recaptcha validation in java
  17. google recaptcha validation ajax
  18. google recaptcha verify
  19. google recaptcha verify response javascript
  20. google recaptcha check

Here is my JSON array: var planets = [{ "Name": "Mercury", "Temperature": "427°C", "Position" : 1 }, { "Name": "Venus", "Temperature": "462°C", "Position" : 2 }, { "Name": "Earth", "...

Here is my JSON array: var planets = [{ "Name": "Mercury", "Temperature": "427°C", "Position" : 1 }, { "Name": "Venus", "Temperature": "462°C", "Position" : 2 }, { "Name": "Earth", "...

  1. pandas return index location
  2. python return index location

I'm doing some programmatic drawing in Highcharts using Highcharts.Renderer using path() and rect(). In the code below I have manually plotted the coordinates for the line and rect. In reality the are ...

I'm doing some programmatic drawing in Highcharts using Highcharts.Renderer using path() and rect(). In the code below I have manually plotted the coordinates for the line and rect. In reality the are ...