简体   繁体   English

如何回显mysqli导致div具有javascript函数的onclick,其中包含另一个onclick用于不同的javascript函数

[英]How to echo mysqli results in a div with an onclick for javascript function which contains another onclick for different javascript function

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. 我正在使用php / mysqli从数据库中获取信息,并在预期的div中通过指向JavaScript函数的onclick对其进行回显。 This all works great. 这一切都很好。 The problem I am having is the 'innerHTML' includes an onclick pointing to a javascript function also; 我遇到的问题是“ innerHTML”还包括指向JavaScript函数的onclick。 this doesn't seem to work. 这似乎不起作用。 Is it possible to have a document.getelementbyid('somthing').innerHTML that contains a function call? 是否可能有一个包含函数调用的document.getelementbyid('somthing')。innerHTML? 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. 现在已经更新,可以在任何人需要的情况下用作参考。

In the external script variables id2 & id3 needed to be defined. 在外部脚本中,需要定义id2id3变量。

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

Also the last innerHTML statement was missing the operator + . 另外,最后一个innerHTML语句缺少运算符+

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

The PHP is echoing a script function showplay(img) ; PHP正在回显脚本function showplay(img) this can be moved outside of the php and still work. 可以将其移到php之外,并且仍然可以使用。 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; 如果留在PHP中,它将在页面上放置相同脚本的次数与数据库中结果的次数相同; this script only needs to be included once and not multiple times. 该脚本只需要包含一次即可,无需多次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM