K
K
KirillKenigsberg2021-06-07 11:42:46
PHP
KirillKenigsberg, 2021-06-07 11:42:46

Like button in php+js?

Hello! I’m making a like button on the site, for some reason it doesn’t work. The button is always white and nothing is written to the database (the connection is exactly right). I can at least solve the color problem.
Here is the click handler code:

<?session_start()?>
<script>
$('.heart')[0].addEventListener('click', function(){
        
        if (<? echo "'".$_SESSION['color'.$_GET['id']]."'"?> == 'white'){<?
            $sql = 'SELECT likes FROM login_cupa WHERE id='.$_GET['id'];
            include 'mysql/mysql_connect.php';
            $likes = mysqli_query($mysql, $sql);
            $like = mysqli_fetch_all($likes,  MYSQLI_ASSOC)[0]['likes'];
            $sql = 'UPDATE login_cupa SET likes='.++$like.' WHERE id="'.$_GET['id'].'"';
            mysqli_query($mysql, $sql);
            $_SESSION['color'.$_GET['id']] = 'red';
            ?>location.reload();
            return
            
        }
        else if(<? echo "'".$_SESSION['color'.$_GET['id']]."'"?> == 'red'){<?
            $sql = 'SELECT likes FROM login_cupa WHERE id='.$_GET['id'];
            include 'mysql/mysql_connect.php';
            $likes = mysqli_query($mysql, $sql);
            $like = mysqli_fetch_all($likes,  MYSQLI_ASSOC)[0]['likes'];
            $sql = 'UPDATE login_cupa SET likes='.--$like.' WHERE id="'.$_GET['id'].'"';
            mysqli_query($mysql, $sql);
            $_SESSION['color'.$_GET['id']] = 'white';
        ?>console.log(1)
        
        }
    console.log(<?echo "'".$_SESSION['color'.$_GET['id']]."'"?>);
    
});
</script>

And here is the button itself:
<a class="under_photo heart">Добавить в избранные <i class="far fa-heart"></i></a>
                            <a class="under_photo">Отправить открытку <i class="far fa-address-card"></i></a>
                            <? include "php/like_post.php" ?>
                            <? 
                            print_r($_SESSION);
                            if ($_SESSION['color'.$_GET['id']] == 'white'){
                                
                                ?>
                                <script>
                                    $('.heart').css('color', 'white');
                                    $('.heart').css('background', '#0075ff');
                                </script>
                                <?}
                                if ($_SESSION['color'.$_GET['id']] == 'red'){
                                    ?>
                                    <script>
                                        $('.heart').css('color', 'white');
                                        $('.heart').css('background', 'red');
                                    </script>
                                    <?
                                }
                            ?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yupiter7575, 2021-06-07
@yupiter7575

php code is executed once on the server, not whenever you want.

L
Lone Ice, 2021-06-07
@daemonhk

PHP separately, HTML separately, JS separately, then smoke what is AJAX

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question