0

How to correctly place a variable from js in php code? I would like to use a variable carsColor instead '#ffcc80'. Thanks in advance!

<script>
var carsColor = '#ffcc80';
</script>

<?php
$colors = array('Cars'=> '#ffcc80', 'Bike' => '#a5d6a7');
?>
Goffer
  • 266
  • 1
  • 2
  • 10
  • 1
    You can't, because that's not how it works. PHP has no idea JS is here, and JS has no idea PHP is here. PHP runs on a server, JS on a client, on two very distinct machines and environments. – Jeremy Thille Aug 15 '18 at 13:58
  • But if you're using inline script like this, you can echo php vars into the javascript. If you're just trying to reuse variables, define them in your server side, echo them into client side. I assume this is a small project. Don't do this with a large code base, you'll never find and organize things that way. – Nikki9696 Aug 15 '18 at 14:29
  • Thank you Jeremy and Nikki. – Goffer Aug 22 '18 at 11:09

0 Answers0