I am trying to pass 2 variables from PHP to JavaScript and the vice versa.
I understand how to pass one variable, and it's okay, but when I tried to pass 2 variales, one of them passed, the other one didn't work.
Here is the code:
(1) file1.php:
<script type="text/javascript">
var id = <?php echo $id ; ?>
var q= <?php echo $q ; ?>
</script>
<script type="text/javascript"src="http://localhost/site/js/java.js"> </script>
(2) Java.js:
http://localhost/site/file2.php?id="+id +"&q="+q
(3) file2.php:
$id = $_GET['id']; >> it works fine
$q= $_GET['q']; >> doesn't pass (error: undefined)
I am not sure where the problem is, I just think it's in the number (2) step.
Any help will be appreciated.
As per comments, if q is a string then it needs to be enclosed in quotes. Remember all you php code is doing is generating text.
var q= <?php echo $q ; ?>
Should be
var q= "<?php echo $q ; ?>";
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.