admin管理员组

文章数量:1295909

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

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 edited Jun 28, 2012 at 10:05 Charmie asked Jun 28, 2012 at 9:46 CharmieCharmie 2,6307 gold badges34 silver badges47 bronze badges 5
  • 1 What you did is correct Wrap that with <script type="text/javascript"> </script> – madhairsilence Commented Jun 28, 2012 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 Commented Jun 28, 2012 at 9:49
  • something like an inline javascript? – Charmie Commented Jun 28, 2012 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 Commented Jun 28, 2012 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 Commented Jun 28, 2012 at 9:53
Add a ment  | 

2 Answers 2

Reset to default 8

Just output this:

echo '<script type="text/javascript">callMe()</script>';

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 :)

本文标签: calling Javascript function without any button to trigger it in PHPStack Overflow