dsfgdsjfd78773 2018-03-10 22:41
浏览 53
已采纳

如何使用onclick为javascript函数回显mysqli结果,其中包含另一个用于不同javascript函数的onclick

I can't seem to find out the correct way to write this script; can someone please point me in the right direction, or offer an alternate way. I have looked but cannot find any relative examples. I am pulling info from a database with php/mysqli and echoing it in prospective divs with an onclick pointing to a javascript function. This all works great. The problem I am having is the 'innerHTML' includes an onclick pointing to a javascript function also; this doesn't seem to work. Is it possible to have a document.getelementbyid('somthing').innerHTML that contains a function call? I am not sure if I asked that write. The code is below. Thanks for any help.

    <?php
    $sql50 = "SELECT * FROM videos ORDER BY id DESC";
    $result50 = mysqli_query($conn, $sql50);
    if (mysqli_num_rows($result50) > 0) {
        while($row = mysqli_fetch_assoc($result50)) {
    $videopath = $row["videopath"];
    $posterpath = $row["posterpath"];
    $user = $row["user"];
    echo "
    <div class='vidresponsive'><div class='vidgallery'>
    <img onclick='showplay(this)' data-id='$videopath' data-id1='$posterpath' data-id2='$user'
    data-id3=".$_SESSION["myspot"]." src='" . $row["posterpath"] . "' width='300px' height='160px' id='next' name='next'>
    <div class='viddesc'>" . $row["title"] . "</div></div></div>";
    echo "
    <script>
     function showplay(img) {
    var id= img.getAttribute('data-id');
    var id1= img.getAttribute('data-id1');
    var id2= img.getAttribute('data-id2');
    var id3= img.getAttribute('data-id3');
      document.getElementById('crab').style.display='none';
       document.getElementById('sticky').innerHTML = \"<div class='controls'>\
    <video style='width:60vw;' controls autoplay poster='\" + id1 + \"'>\
    <source src='\" + id + \"' type='video/mp4'>\
    </video>\
    <br><span id='saywhat' onclick='liking();' class='point'><i id='thumbing' class='fa fa-thumbs-up notlike'></i></span>\
    </div>\"
    }
    </script>";
        }
    } else {
        echo "Sorry, there's nothing here.";
    }
    mysqli_close($conn);
    ?>
    <div class="vidclearfix"></div>
    <script>
    var id2= img.getAttribute('data-id2');
    var id3= img.getAttribute('data-id3');
    function liking() {
     $.ajax({
       url:'actions/like.php',
        type:'POST',
        data:{
            id2: id2, id3: id3
        }
    });
       document.getElementById('saywhat').innerHTML += "Thank You";
       document.getElementById("thumbing").classList.remove('notlike');
       document.getElementById("thumbing").classList.add('like');
    }
    </script>

I just can't seem to get the function 'liking' to work, it's like it's non-clickable and nothing happens when i do click it. A nudge in the right direction would be greatly appreciated. Thank you very much.

This has been updated now and works if anyone needs any of it for a reference.

  • 写回答

1条回答 默认 最新

  • douruhu4282 2018-04-07 16:05
    关注

    In the external script variables id2 & id3 needed to be defined.

        var id2= img.getAttribute('data-id2');
        var id3= img.getAttribute('data-id3');
    

    Also the last innerHTML statement was missing the operator +.

        document.getElementById('saywhat').innerHTML += "Thank You";
    

    The PHP is echoing a script function showplay(img); this can be moved outside of the php and still work. If left inside of the PHP it will place the same script on the page as many times as there are results in the database; this script only needs to be included once and not multiple times.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog