Load PHP function with jQuery Ajax

Posted by brandon14_99 on Stack Overflow See other posts from Stack Overflow or by brandon14_99
Published on 2010-05-10T07:44:45Z Indexed on 2010/05/11 15:14 UTC
Read the original article Hit count: 238

Filed under:
|
|
|

I have a file which is loaded at the top of my document, which is called Videos.php. Inside that file are several functions, such as getYoutubeVideos. On some pages, I need to call upon that function several times (up to 50), and it of course creates major lag on load times. So I have been trying to figure out how to call that function in, only when it is need (when someone clicks the show videos button). I have very little experience with jQuery's ajax abilities. I would like the ajax call to be made inside of something like this:

           jQuery('a[rel=VideoPreview1).click(function(){
                jQuery ("a[rel=VideoPreview1]").hide();
                jQuery ("a[rel=HideVideoPreview1]").show();
                jQuery ("#VideoPreview1").show();

                                //AJAX STUFF HERE

                preventDefault();
            });

Ok I have created this based on the responses, but it is still not working:

jQuery Code:

        jQuery(document).ready(function(){ 

            jQuery("a[rel=VideoPreview5]").click(function(){
                jQuery("a[rel=VideoPreview5]").hide();
                jQuery("a[rel=HideVideoPreview5]").show();
                jQuery.post("/Classes/Video.php", {action: "getYoutubeVideos",
artist: "Train", track: "Hey, Soul Sister"},
                    function(data){
                        jQuery("#VideoPreview5").html(data);
                    }, 'json');
                jQuery("#VideoPreview5").show();
                preventDefault();
            });
            jQuery("a[rel=HideVideoPreview5]").click(function(){
                jQuery("a[rel=VideoPreview5]").show();
                jQuery("a[rel=HideVideoPreview5]").hide();
                jQuery("#VideoPreview5").hide();
                preventDefault();
            });
        });


And the PHP code:

    $Action = isset($_POST['action']);
    $Artist = isset($_POST['artist']);
    $Track = isset($_POST['track']);
    if($Action == 'getYoutubeVideos')
    {
        echo 'where are the videos';
        echo json_encode(getYoutubeVideos($Artist.' '.$Track, 1, 5, 'relevance'));
    }

© Stack Overflow or respective owner

Related posts about php

Related posts about functions