Insert embed video with mysql

Asked

Viewed 356 times

2

Good afternoon, I am developing a website to simplify the search for educational videos for my TCC.

I made a system with two combo box to search the disciplines in the database and the contents, being contents a key Foreign of disciplines.

<h2 class="title">Selecione a disciplina</h2>
        <form action="" method="get">
        <div class="box">
          <select name="disciplina" id="disciplina">
            <option value="" selected = selected>Selecione uma disciplina</option>
              <?php
              if($num_logar > 0) {
                  do {
                  echo "<option value='".$fet_logar['disciplina_id']."'>".$fet_logar['disciplina_nome']."</option>";
                  }while($fet_logar = mysqli_fetch_assoc($exe_logar));
              }
              ?>
        </select>
            </div>
        </div>
        <div class="col-md-4 offset-md-4">
        <h2 class="title">Selecione o conteúdo</h2>
        <div class="box">
        <select name="conteudo" id="conteudo">    
        </select>
        </form>
        <ul class="dropdown-menu">
        <script type="text/javascript">
        $(document).ready(function(){
            $('#disciplina').change(function(){
                $('#conteudo').load('conteudo.php?disciplina='+$('#disciplina').val());

            });
        });
        </script>
        </ul>
        </div>

inserir a descrição da imagem aqui

That is, after the user selects a course, their respective contents will appear in another combo box, being made with javascript.

But since the user selected the content I want to appear automatically, searched from mysql, the link of the video embed youtube.

I did this in a non-automatic way for a representation. Follow the code below.

<figure>
  <div class="boxVideo">
  <iframe  width="700" height="450" src="https://www.youtube.com/embed/F9Bo89m2f6g" frameborder="0" allowfullscreen></iframe>
  </div>
  </figure>

How do I automatically embed the video link, after selecting the content of a particular discipline, appears below the page?

Illustration from the comic book: Table discipline, as well as the combo box that demonstrates the disciplines

Tabela disciplina, assim como o combo box que demonstra as cadastradas

Table content, with a FK, so that when selecting the discipline the content to be shown will prevail from which discipline was selected

Tabela conteúdo, com uma FK, para que ao selecionar a disciplina o conteúdo a ser mostrado prevalecerá a partir de qual disciplina foi selecionada

OBSERVING: From a previous answer, it was observed that:

1- The content select is pulled from a javascript, that is, the value of it equals the content_id and following the value name.

2- An idea of solution would be a field as I did on the table "content", video content, that in this field would enter the embed code of the video, example "wjwudahw12" that would enter in "youtube.com/embed/wjwudahw12", as I can edit the "frame", for it to traverse through this field, after selecting discipline and content, and insert the embed code respective.

1 answer

1


Friend, it seems that the problem lies in solving client-side programed server-side.

Are you trying to upload content within the select, to do this use the function $.get of jQuery:

var conteudos = $.get('conteudo.php?disciplina='+$('#disciplina').val());

This page conteudo.php must return a JSON:

[
  {
    value: "F9Bo89m2f6g",
    name: "Video 1"
  },
  {
    value: "AF7M17QX4QE",
    name: "Video 2"
  }
]

For so, you get popular the select in this way:

    $.each(conteudos, function (key, conteudo) {
      $("#conteudo")
        .append(
          "<option value=" + conteudo.value + ">" + conteudo.name + "</option>"
        );
    });

To show the video you can use the value of select of the contents, thus:

$("#conteudo").change(function () {
    var codigoVideo = $("#conteudo").val();
    var videoContainer = $("#video_container");
    videoContainer.html(`
    <iframe width="700" height="450" src="https://www.youtube.com/embed/${codigoVideo}" frameborder="0" allowfullscreen></iframe>`);
  });

Here’s an example working: https://codepen.io/kleberoliveira/pen/gOpNwYP

  • Opa @Kleber Oliveira, thanks for the help! I could understand, and that’s just what I need! But how can I synchronize so that the registered videos are pulled from mysql?

  • @Matheus you need the [content.php] file to pull from the mysql database this information. I believe that using the PDO lib and a simple SELECT query, and using the json_encode PHP solve this problem. Don’t forget to evaluate the help and close the question in stackoverflow.

  • You used js to show the video there, right? like: { value: "F9bo89m2f6g", name: "Video 1" },

  • How does it get to pull this value from the database? I don’t understand very well of js

  • you have a field conteudo_video use it to save the youtube video code. ex: "F9bo89m2f6g"

  • Sure. I will register all the codes there. But to insert them in js? How does it look?

  • var conteudos = $.get('conteudo.php?disciplina='+$('#disciplina').val()); create a file conteudo.php that the result is like the JSON nominee.

  • would you do for me? am I not experienced mt in js, to boiando too much kk

  • is not a problem of JS, but I did the code PHP link: https://repl.it/@Kleberoliveira/Simplepdoselect

  • I forwarded you the php code in chat.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.