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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)