0

Hello, I am making a jQuery YouTube search and display web application and I need to send the link of a specific video to the database using PHP.

In the application, the user searches for a video and the unique code for that video gets inserted into a set YouTube link using an "id" variable. I want to capture the full link (including the unique id) and put it into the database without having to open a new page (which is how AJAX does it, I think).

Here are links to some similar problems, but they haven't worked for me. (getting youtube video id the PHP, How do I find all YouTube video ids in a string using a regex?, https://stackoverflow.com/questions/19977278/how-to-get-php-data-in-javascript, Data transfer from JavaScript to PHP, http://www.webdeveloper.com/forum/showthread.php?249863-pass-jquery-value-to-php-variable (sorry for the massive amount of links, Stack told me to include them if they were similar and didn't work))

Here is the code that makes it work so far.

<script type="text/javascript">
//on thumbnail click
$videoDiv.click(function(e) {
    displayVideo(entry.media$group.yt$videoid.$t, entry.title.$t);
});


//Display the video
function displayVideo(id, title) {
//embed player
    swfobject.embedSWF('http://www.youtube.com/e/' + id + '?enablejsapi=1&playerapiid=ytplayer&autoplay=1',
    'video-placeholder', '544', '408', '9.0.0', null, null, { allowScriptAccess: "always" },
    { id: "youtubevideo" } );
}
</script>

Obviously, this isn't the whole function, but hopefully it should be enough.

Please help me, I've run out of places to look.

Hi Niklas,

Thank you for answering me, I’ve tried that out and it turns out that my code is a bit more complicated to allow for that.

My project for university also needs to have information sent to the page that the video is on, currently I have the form for the user on one page and the code to search YouTube and display the user’s information as well as the video on the next (external) page.

Unfortunately AJAX is still one of the languages I haven’t yet learnt, so the documentation you linked me to didn’t make much sense.

My code is very complicated as it uses the YouTube API as well as JavaScript, jQuery and PHP. The main js file for this searches YouTube and once the user clicks on the thumbnail, it fires up a displayVideo function, which also features the information from the previous page.

I can’t redirect the user to a new page after they select the video, just to get the id into PHP, because that would wipe the data from the first form when the user gets redirected back to the YouTube form.

I’m hoping you could please assist me.

Community
  • 1
  • 1
Natasha
  • 15
  • 2

1 Answers1

0

Answer

Okey, know I understand what you're asking. That you would do something like this:

<script type="text/javascript">
$(function() {
    //on thumbnail click
    $videoDiv.click(function(e) {
        var videoURL = displayVideo(entry.media$group.yt$videoid.$t, entry.title.$t);
        $.post('url-to-your-php-file', {videoURL: videoURL}, function(response) {
            // Do stuff with the response given/echoed by the PHP file
        });
    });


    //Display the video
    function displayVideo(id, title) {
        //embed player
        var videoURL = 'http://www.youtube.com/e/' + id + '?enablejsapi=1&playerapiid=ytplayer&autoplay=1';
        swfobject.embedSWF(videoURL,
        'video-placeholder', '544', '408', '9.0.0', null, null, { allowScriptAccess: "always" },
        { id: "youtubevideo" } );
        return videoURL;
    }
});
</script>

Hope this helps and good luck!


Answer before revised question

I won't write the code for you, but you're right in your thinking and I will give you some links that may help. You need to make an AJAX call to a PHP file which saves the link to the database.

AJAX Call

You're using jQuery, which makes AJAX calls real easy. Have a look at the jQuery .ajax() documentation. Though in this case it being a really small and "unadvanced" call you need to make, the shorthand wrapper function .post() will do nicely for you.

Retrieve the link in PHP

After that you can retrieve the link in the PHP using the global $_POST variable.

Adding the link to the database

For adding it to the database, I would recommend the PHP PDO library for making the connection and queries.

Hope this helps and don't hesitate to ask if there's anything you wonder!

Niklas
  • 1,729
  • 1
  • 12
  • 19
  • Thanks Niklas, please see revised question above. – Natasha Jul 27 '14 at 10:30
  • My answer is now updated. Please make an edit of your actual question (not just the title) as well. This will allow me to remove my down vote and will make the question even more clear to others that may read it. – Niklas Jul 27 '14 at 10:39
  • Edited once more. Added `$(function() { ... });` wrapper around the code so it waits until the document is ready. – Niklas Jul 27 '14 at 10:43
  • Thank you so much Niklas, it worked brilliantly. :D I would vote your answer, but I don't have enough reputation yet. :) – Natasha Jul 29 '14 at 13:52
  • That's okey, got 15 reps. for you accepting my answer :) But sure, as soon as you have enough reputation, feel free to upvote it as well ;) – Niklas Jul 29 '14 at 14:25