Sathya Raj
Sathya Raj

Reputation: 1089

Can I trigger a javascript function from a php function?

I am getting html data through js ajax function like this

function getData(dataSource,datasend)
{ 

var XMLHttpRequestObject = false; 

if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
} else if (window.ActiveXObject) {
XMLHttpRequestObject = new 
ActiveXObject("Microsoft.XMLHTTP");
}

if(XMLHttpRequestObject) {
XMLHttpRequestObject.open("POST", dataSource, true); 
XMLHttpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
XMLHttpRequestObject.send('data='+escape(datasend));

XMLHttpRequestObject.onreadystatechange = function() 
{ 
    if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { 
        document.getElementById(datafin).innerHTML = XMLHttpRequestObject.responseText;
            //when the html has require data extecute this function
            if(html has needed data for the function){
                foo();
            }

    }
} 

function foo(){
     //stuff to do with the html when loaded
}

as far as i do it the html is generated by a php function. Now Is there way to trigger the foo() function in the html data generated or a way to send data that can trigger the function. Thanks

Upvotes: 0

Views: 167

Answers (4)

Sathya Raj
Sathya Raj

Reputation: 1089

I tried to trigger the js function from php but in vein but i have achieve it by giving a dummy div in html data and checked it in success if present and triggered the function like this

XMLHttpRequestObject.onreadystatechange = function() 
{ 
    if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { 
        document.getElementById(datafin).innerHTML = XMLHttpRequestObject.responseText;
            //when the html has require data extecute this function
            if($('#foo_div').length > 0){
                foo();
            }

    }
} 

function foo(){
     //stuff to do with the html when loaded
     //remove div after everything finished
     $('#foo_div').remove();
}

I still don't think that this is the properway to do it. Any Proper answer i'll greatly appreciate it.

Upvotes: 0

Krish R
Krish R

Reputation: 22741

You can use like,

in php file,

 echo '<script>foo();</script>';

javscript,

  function foo(){
     //stuff to do with the html when loaded
     alert('test');
  }

Upvotes: 0

jvinces
jvinces

Reputation: 21

assuming that php,html,javascript is in the same file

<?php
$stuff_to_do = "anything";
$desicion_variable = ($access_db_or_anything)?"needed_foo":"dont_needed_foo";
.....
?>
<!-- html -->
<script>
function getData(dataSource,datasend)
{ 
......
    if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { 
        document.getElementById(datafin).innerHTML = XMLHttpRequestObject.responseText;
            //when the html has require data extecute this function
            <?php 
            // condition_html_has_needed_data_for_the_function
            if ( $desicion_variable == "needed_foo" ){
                ?>
                foo();
                <?php
            }
            ?>
    }
}
function foo(){
     //stuff to do with the html when loaded
}
</script>

and if you have file js is separate of html and php: page_example.php events.js page_example.php

<?php
$stuff_to_do = "anything";
$desicion_variable = ($access_db_or_anything)?"needed_foo":"dont_needed_foo";
.....
?>
<body data-optionFoo="<?php echo $desicion_variable ?>">
    <!-- html stuff -->
</body>
<script src="events.js"></script>

events.js

function getData(dataSource,datasend)
{ 
   .....
    if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { 
        document.getElementById(datafin).innerHTML = XMLHttpRequestObject.responseText;
            //when the html has require data extecute this function
            var objDiv = document.getElementById("pnlOpcion");
            if ( objDiv.getAttribute('data-optionFoo') == "needed_foo" ){
                foo();
            }
    }
}
function foo(){
     //stuff to do with the html when loaded
}

Upvotes: 0

Mahmood Rehman
Mahmood Rehman

Reputation: 4331

you can do like this :

    <?php 

        echo "<script type='text/javascript'> function foo()
{ alert('my home');} foo();</script>";echo "<script type='text/javascript'> foo()</scipt>";
    ?>

Upvotes: 1

Related Questions