i want to do something like this how do you do it?
<?php
$meat="cow";
<script type="text/javascript">
document.write($meat);
</script>
?>
i want to do something like this how do you do it?
<?php
$meat="cow";
<script type="text/javascript">
document.write($meat);
</script>
?>
hi just add another variable on the js which contain the php variable like this
<?php
$meat="cow";
?>
<script type="text/javascript">
var meat="<?php echo $meat; ?>";
document.write(meat);
</script>
Sorry I know this topic is 2 months old but this might help someone. Always encode JS variables with JSON when creating a JS variable from a PHP variable. See below:
instead of: var meat="<?php echo $meat; ?>";
do this: var meat="<?php echo json_encode($meat); ?>";
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.