i want to do something like this how do you do it?

<?php
$meat="cow";
<script type="text/javascript">
document.write($meat);
</script>
?>

Recommended Answers

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); ?>";

Jump to Post

All 2 Replies

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); ?>";

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.