-1

I have js function that triggered when I do something, and I want to call a php function. I know I need to use ajax to do so, but how exactly can I do it? I learn ajax, but I didn't find reference for it. Can you plz show me how its work. This is the basic of how I want it to work:

JS:call php function when JS triggered.

PHP:echo(or do) something.

EDIT: what I tring to get is infinite-scroll, that when I get to the bottom of the page it give me new php data. I follow this episode, and I want insdead of blue squares, to show data. this is the code of the episode. When the page to to the bottom it display blue square

 <!DOCTYPE html>
    <html>
    <head>
    <script>
    function yHandler(){
        // Watch video for line by line explanation of the code
        // http://www.youtube.com/watch?v=eziREnZPml4
        var wrap = document.getElementById('wrap');
        var contentHeight = wrap.offsetHeight;
        var yOffset = window.pageYOffset; 
        var y = yOffset + window.innerHeight;
        if(y >= contentHeight){
            // Ajax call to get more dynamic data goes here
            wrap.innerHTML += '<div class="newData"></div>';
        }
        var status = document.getElementById('status');
        status.innerHTML = contentHeight+" | "+y;
    }
    window.onscroll = yHandler;
    </script>
    <style>
    div#status{position:fixed; font-size:24px;}
    div#wrap{width:800px; margin:0px auto;}
    div.newData{height:1000px; background:#09F; margin:10px 0px;}
    </style>
    </head>
    <body>
    <div id="status">0 | 0</div>
    <div id="wrap"><img src="temp9.jpg"></div>
    </body>
    </html>
4
  • Need a more detailed explanation of expectations and problems you are having implementing the ajax. Also show us some code. You are aware that php only runs at the server and javascript runs in the browser correct? Commented Aug 2, 2015 at 13:23
  • possible duplicate of Calling PHP file using AJAX Commented Aug 2, 2015 at 13:26
  • yes i understand that. I will update my question Commented Aug 2, 2015 at 13:26
  • @AhmedShamel in this question that you linked, does the "new-user.php" is a diffrent page in the same folder. I mean did they make an html-js page and linked to php page? Commented Aug 2, 2015 at 13:30

1 Answer 1

0
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div id="fooid">click me!</div>
<script>
$( "#fooid" ).click(function() {
   var term = "some string foo";
  // Send the data using post, in PHP you will get $_POST['foo']
  var posting = $.post('index.php', { foo: term } );
  // alert the results (in php script: echo "test performed!";)
  posting.done(function( data ) {
   alert( "Data Loaded: " + data );
  });
});
</script>

</body>
</html>
Sign up to request clarification or add additional context in comments.

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.