Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Well, before I proceed to another phase of my coding, just to make sure im not going to bark at the wrong tree, may I ask if it is possible to call a javascript function in php without any button click action to trigger it. i also dont consider onload() or onready() because im going to call the javascript function inside a foreach loop in php.. well the concept goes somehow like this:

<script type="text/javascript">
   function callMe(){
      alert('im called!');
   }
</script>

<body>
  <?php
    .....
    foreach($somevar as $var){
     // assuming it will loop 5 times
    callMe();  // well this is the part where im going to call the javascript function
   }
  ?>
</body>

Thank you in advance.

******EDITED PART*****

here's the actual codes i plan to use:

function AddCoordinate( lat, long )                                                                                                                                   
{
  var path = LinePath.getPath(); 
  path.push( new google.maps.LatLng( lat, long ) );
}


<?php    
    foreach($arrayOfPlotPoints as $key => $value){ 
        $longitude = round($value['longitude'],5);
        $latitude = round($value['latitude'],5);
        $snrLevel = $value['snr_level'];
        echo '<script         
            type="text/javascript">AddCoordinate('.$latitude.','.$longitude.')</script>

?>

actually that's the right answer..it already worked.. so im gonna provide an answer as well

share|improve this question
1  
What you did is correct Wrap that with <script type="text/javascript"> </script> –  madhairsilence Jun 28 '12 at 9:48
1  
Could you explain what are you trying to achieve? Right now this is not making much sense to do it like this. –  slash197 Jun 28 '12 at 9:49
    
something like an inline javascript? –  Charmie Jun 28 '12 at 9:50
    
im actually going to pass the variables to a javascript function and store it in a javascript array.. that is why right there and then when the values are being exploded in the foreach loop, im going to pass it to the javascript function... i dont want to do it the way like this: store to a variable and pass to javascript –  Charmie Jun 28 '12 at 9:52
2  
It seems like you are confusing server-side functionality (PHP) with client-side functionality (Javascript). Could you edit your question to explain what you want to achieve and then how you have attempted to achieve it? –  Andrew Leach Jun 28 '12 at 9:53
show 1 more comment

2 Answers

up vote 6 down vote accepted

Just output this:

echo '<script type="text/javascript">callMe()</script>';
share|improve this answer
add comment

thanks to Donatas.. now this is what i have:

function AddCoordinate( lat, long )                                                                                                                                   
{
  var path = LinePath.getPath(); 
  path.push( new google.maps.LatLng( lat, long ) );
}


<?php    
    foreach($arrayOfPlotPoints as $key => $value){ 
        $longitude = round($value['longitude'],5);
        $latitude = round($value['latitude'],5);
        $snrLevel = $value['snr_level'];
        echo '<script         
            type="text/javascript">AddCoordinate('.$latitude.','.$longitude.')</script>

?>

it works :)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.