Calling the PHP function from javascript in the same file using ajax

advertisements

So, I'm trying to call a php function from the same file where the javascript lies.

I'm trying to call the php function every 10 sec to add new items to the "li"

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">

function addElement () {
var newLi = document.createElement("li");
var text = document.createTextNode("Teststring in <li>");

newLi.appendChild(text);
var ulnew = document.getElementsByClassName('new-item');
ulnew[0].appendChild(newLi)
}
window.onload = function(){setInterval(function(){
    $.ajax( // I just copy pasted this part to
           {
               url: 'test.php',
               type: 'GET',
               success: function(output)
               {
                      //alert(output);
                      document.write("It worked!");
               }
            }
        );// here I really have no idea what is suppose to happen here
},10000);}
</script>

<?php 

function echoTest(){
   echo 'item';
}

?>

Again, What I'm trying to do is Execute a php function every 10 sec. How am I going to do this? Please Help!


    Try This

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

    <script>
    window.onload = function(){setInterval(function(){
        $.ajax( // I just copy pasted this part to
               {
                   url: 'http://localhost/test.php',
                   type: 'GET',
                   success: function(output)
                   {
                        var current = $("ul#updating-list").html();
                        $("ul#updating-list").html(current + "<li>" + output + "</li>");

                   }
                }
            ); // here I really have no idea what is suppose to happen here
    },10000);}
    </script>

and you need to call the function in php

<?php 

function echoTest(){
   echo 'item';
}
echotest();
?>