0

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

5
  • 1
    What you did is correct Wrap that with <script type="text/javascript"> </script> 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. Commented Jun 28, 2012 at 9:49
  • something like an inline javascript? 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 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? Commented Jun 28, 2012 at 9:53

2 Answers 2

8

Just output this:

echo '<script type="text/javascript">callMe()</script>';
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.