Skip to content Skip to sidebar Skip to footer

JS Var To Get PHP Value In WP

I was reading all similar questions and answers around, but none seem to work for me. i have this:

Solution 1:

The issue is that you have quotes around your echo statement, you need to have them around your variable AFTER it has been output to function within the script, like so:

<script>
    var MyCity  = <?php echo '"Cityville"';?>;
</script>

Solution 2:

use like this

<script>
var MyCity  = <?php echo 'Cityville';?>;
</script>

Solution 3:

I think you should encode with json_encode, it will handle single quote or double quote in string.

<?php $city = 'Cityville'; ?>
<script>
  var MyCity = <?php echo json_encode($city); ?>;
</script>

Post a Comment for "JS Var To Get PHP Value In WP"