I'm trying to print my PHP variable:
<?php
$section= $_GET['section'];
$author = $_GET['author'];
?>
<html>
<!--Some HTML page's elements-->
</html>
And my JavaScript code:
$(document).ready(function(){
var SECTION = '<?php echo $section;?>';
alert('My section is: ' + SECTION);
var AUTHOR = '<?php echo $author;?>';
alert('Current author is: ' + AUTHOR);
});
But only I get in alert's message: 'My section is: ' + <?php echo $section;?>
'
Try running this all a single page
test.php?section=5&author=james
test.php
<?php
$section= $_GET['section'];
$author = $_GET['author'];
?>
<html>
<!--Some HTML page's elements-->
</html>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
var SECTION = '<?php echo $section;?>';
alert('My section is: ' + SECTION);
var AUTHOR = '<?php echo $author;?>';
alert('Current author is: ' + AUTHOR);
});
</script>
works fine
The answer provided by Rob will set your JS variables from PHP, but also makes you vulnerable to Javascript injection - Do not use this method as is.
If you want to set JS variables from within PHP, and those variables should be set by the user - simply set them from within JS, or do a very strict input filtering.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.