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 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析