-7

I am using the following code:

       <?php  
         $id = $_GET['id'];
         echo $id; 
            var java_id= <?php echo $id; ?> ;
             alert(java_id);


    </script>
The value doesn't alert. What is the mistake?

Thank you!

naveen
  • 87
  • 1
  • 3
  • 10

3 Answers3

0

Maybe try just this:

<script>
         // More java code here I am guessing from your example...
         var java_id= <?php echo $_GET['id']; ?> ;
         alert(java_id);
</script>
Laurence
  • 58,936
  • 21
  • 171
  • 212
0
<?php  
  $id = $_GET['id'];
?>

<script>
  var java_id= <?php echo $id; ?> ;
  alert(java_id);        
</script>

Close properly, your php and javascript code, those are two different language.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Sudip Pal
  • 2,041
  • 1
  • 13
  • 16
0

if you are writing your code on a .php file then javascript variables need to be enclosed within tag and there you can use php variables directly by assinging variable in your case like var java_id= ""; inside script tag... however if you are trying to directly access php variable in your .js file then you need to create global javascript variable before including your .js file...

jseru
  • 31
  • 4