Michael
Michael

Reputation: 183

What would i use as a get request in jquery/ajax to set parameters

i am new to Jquery/Ajax and i am trying to have the source url for the json change based on the url parameters i setup, i have working version in PHP, but i don't know how to write it in JQuery

This is my PHP Code (what i am currently using

  $id = urlencode($_GET['id']);
       $page = urlencode($_GET['results']);
    $url = "https://gdata.youtube.com/feeds/api/playlists/$id?alt=jsonc&v=2&max-results=25&&start-index={$results}";

This code grabs the id and includes it to alter the url of the source file used in the script

so how would i make this code act in the same way?

$(document).ready(function() {
    startindex = 1;
    loadmore = 20;
    addMore(startindex, loadmore);

    $('#addmore').on('click',function(e) {
        e.preventDefault();
        addMore($('#list li').length, 20);
    });
});

function addMore(startindex,loadmore) {

    src = "https://gdata.youtube.com/feeds/api/playlists/ID_WOULD_GO_HERE?alt=json&max-results=" + loadmore + "&start-index=" + startindex;

    $.ajax({
        dataType: "jsonp",
        url: src,
        success: function(data, textStatus, jqXHR) {
            if (data.feed && data.feed.entry) {
                var $list = $('#list');

                $.each(data.feed.entry, function(i, e) {
                    $list.append('<li class="video"><a href="' + e.link[1].href + '"><img src="'+ e.media$group.media$thumbnail[0].url +'" width="250"></img></a><br>' + e.title.$t + '<P>' + e.author[0].name.$t + ' | '+ e.yt$statistics.viewCount +' Views</span></li>');
                });
            }
        }
    });
}

Please help, Thanks!

Upvotes: 0

Views: 169

Answers (1)

Dave Chen
Dave Chen

Reputation: 10975

Please try this:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<div id="list"></div>

<script>
$(document).ready(function() {
    startindex = 1;
    loadmore = 20;
    id = urlVar("id");

    if (id!="") {
        addMore(id, startindex, loadmore);
    }

    $('#addmore').on('click',function(e) {
        e.preventDefault();
        addMore(id, $('#list li').size(), 20);
    });
});

function urlVar(varName) {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars[varName]?vars[varName]:"";
}

function addMore(id, startindex,loadmore) {
    src = "https://gdata.youtube.com/feeds/api/playlists/"+ id +"?alt=json&max-results=" + loadmore + "&start-index=" + startindex;
    $.ajax({
        dataType: "jsonp",
        url: src,
        success: function(data, textStatus, jqXHR) {
            console.log(data);
            if (data.feed && data.feed.entry) {
                var $list = $('#list');
                $.each(data.feed.entry, function(i, e) {
                    $list.append('<li class="video"><a href="' + e.link[1].href + '"><img src="'+ e.media$group.media$thumbnail[0].url +'" width="250"></img></a><br>' + e.title.$t + '<P>' + e.author[0].name.$t + ' | '+ e.yt$statistics.viewCount +' Views</span></li>');
                });
            }
        }
    });
}
</script>

To test: this_script.php?id=RD029cW4vF6U2Dc

Potentially you could also get PHP to put the variable into the URL before hand.

Example:

src = "https://gdata.youtube.com/feeds/api/playlists/<?php echo $_GET['id'];?>?alt=json&max-results=" + loadmore + "&start-index=" + startindex;

Upvotes: 1

Related Questions