如何在javascript函数中传递php值[重复]

This question already has an answer here:

<script>
var test = 1;
<?php
$id = "document.write(test)" ;
//echo $id;
//here i want to run sql code 
?>
var id =<?php echo $id; ?>;
alert(id);
</script>

fist i want pass test value to php id second i wanna pass php id value to var id . when i run this code alert says Undefined . How to display id in a alert.

</div>

You forgot to put quotes. id is a string, and PHP does not echo the quotes when it echoes a string. Change it to:

var id = "<?php echo $id; ?>";