0
1. <script>
2.   document.getElementById('description').innerHTML = description;
3. </script>
4. <div id="description"></div>

How to get the value of line 4 into PHP code?. Also when I use the traditional way

<?php $weather =  <div id = "description"></div>?>

The weather variable is not displayed wherever I want it to be displayed. Any help is greatly appreciated. Thank You.

DeviSowmya
  • 15
  • 4
  • Use AJAX to send the variable to PHP. – Barmar Oct 14 '20 at 16:17
  • When the PHP is running there are no Javascript variables. PHP is running on the _Server_, and its output is going to be sent to the client's _browser_. Whatever variables and computations etc your PHP is doing, what get's sent to the browser is plain old text; there is no Javascript, it's just text. When the browser receives it and processes it, _then_ it's javascript because that's how the browser treats it. But at that time the PHP is _finished running_. It's essentially _gone_ because it's on the server, not the client. PHP and Javascript run at different _times_, on different _machines_. – Stephen P Oct 14 '20 at 21:53

1 Answers1

1

The PHP is processed on the server side, while the Javascript value is processed in the user's browser. So when the page is sent, there is no value there yet.

You can do this:

<?php
$weather = '<div id="description">Details go here</div>'; // define variable
echo $weather; // include / print variable
?>

But if the value does not exist yet that you want to include, then you need to use AJAX to send it to PHP if that's indeed what you need. Although if it doesn't need to go back to the server, it can likely be done just with JavaScript on the client side.

I suggest finding some good books on learning PHP / learning JavasScript / learning HTML.

Ben in CA
  • 688
  • 8
  • 22