css 禁止在单击“喜欢”按钮后重新加载页面

whhtz7ly  于 2023-05-02  发布在  其他
关注(0)|答案(3)|浏览(94)

我编写了代码来like一个出版物,单击like按钮使用服务器端代码将值保存到数据库中。但是,当我单击该按钮时,页面刷新以保存该值,我遇到了一些问题。我尝试使用像event.preventDefault()这样的JavaScript代码,但它不起作用。你能提出一个更好的方法来实现这一点吗?

echo '<form method="POST" action="profile.php" id="likeForm">';
                echo '<input type="hidden" name="id_' . $publication_id . '" value="' . $publication_id . '">';
                echo '<div class="button-container">';
                echo '<button type="submit" id="likeButton" onsubmit="return false;">' . $heart_icon . '</button>';
                echo '</div>';
                echo '</form>';

我试了event.preventDefault(),我也试了这个代码:

echo '<form method="POST" action="profile.php" id="likeForm">';
echo '<input type="hidden" name="id_' . $publication_id . '" value="' . $publication_id . '">';
echo '<div class="button-container">';
echo '<button type="button" id="likeButton">' . $heart_icon . '</button>';
echo '</div>';
echo '</form>';

echo '<script>';
echo 'document.getElementById("likeButton").addEventListener("click", function() {';
echo 'var xhr = new XMLHttpRequest();';
echo 'xhr.open("POST", "profile.php");';
echo 'xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");';
echo 'xhr.onload = function() {';
echo 'if (xhr.status === 200) {';
echo 'console.log(xhr.responseText);'; // or you can show a success message here
echo '}';
echo '};';
echo 'xhr.send("id_' . $publication_id . '=' . $publication_id . '");';
echo '});';
echo '</script>';

最后是这个:

$(function() {
        $('#likeForm').on('submit', function(e) {
            $.ajax({
                type: 'post',
                url: 'profile.php',
                data: $('#likeForm').serialize(),
                success: function() {
                    alert("Data has been submitted!");
                }
            });
            e.preventDefault();
        });
    });
wj8zmpe1

wj8zmpe11#

Preventdefault不适用于提交,您应该使用return false来阻止表单提交。
试着把它也放在你的成功中。

n3schb8v

n3schb8v2#

您需要阻止该事件,以便放弃默认行为。请试试这个。

$(function() {
        $('#likeForm').on('submit', function(e) {
            e.preventDefault();
            $.ajax({
                type: 'post',
                url: 'profile.php',
                data: $('#likeForm').serialize(),
                success: function() {
                    alert("Data has been submitted!");
                }
            });
        });
    });
tkclm6bt

tkclm6bt3#

请在下面找到您编辑的代码并尝试。我试着把它弄得简单些

<form method="POST"  id="likeForm">
        <input type="hidden" name="id_<?php echo $publication_id;?>" value="<?php echo $publication_id;?>">
        <div class="button-container">
    <button type="submit" id="likeButton" onsubmit="return false;"><?php echo  $heart_icon;?> </button>
    </div>
  </form>

<!--jquery -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.4/jquery.min.js" integrity="sha512-pumBsjNRGGqkPzKHndZMaAG+bir374sORyzM3uulLV14lN5LyykqNk8eEeUlUkB3U0M4FApyaHraT65ihJhDpQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<!--custom code -->
<script type="text/javascript">

        $('#likeForm').on('submit', function(e) {
             e.preventDefault();
            $.ajax({
                type: 'post',
                url: 'profile.php',
                data: $('#likeForm').serialize(),
                success: function() {
                    alert("Data has been submitted!");
                }
            });
           
        });
  
</script>

提示和注意事项

1.在JavaScript的情况下开发时,一旦您加载页面,请检查控制台是否有任何错误。
1.因为php能够interprit的html文件,我们可以使代码更可读的注入php的html,而不是做反之亦然,因为我已经做了。
1.当使用jquery或任何其他库时,请确保您的自定义代码总是按顺序排在最后。
1.将所有javascript放在</body>标记的末尾之前是一个很好的做法。

如有疑问或问题,请留言。

相关问题