-2

I was making a 'like system' - like the twitter system - with PHP and AJAX. In a way that I can't understand, it applies to all buttons on the page when I press only one button. Please help.

enter image description here

index.php

<div id="rezbtn">
    <span id="rezarea">
        <button style="font-size:15px; float:left; <? if($sorus["soru_id"] == $rezs['soru_id'] && $user_id == $rezs['user_id']){ echo 'color:green;';}else{ echo ''; } ?>" class="btn rezle" id="rezle" type="button" title="<? echo $sorus["soru_id"]; ?>">
            <span>#Rezle <? echo $rez["rez_id"]; ?></span>
        </button>
    </span>
</div>

ajax.php

if($rezuscount > 0)
    {
        echo "<span style='color:black;'>#Rezle ", $rezcount-1,"</span>";
    }
    else
    {
        echo "<span style='color:green;'>#Rezle ", $rezcount+1,"</span>";
    }

like.js

$('.rezle').click(function(){
    var soruid = $(this).attr("title");
    $.ajax({
        type:"POST",
        url:"ajax.php",
        data:{"soru":soruid},
        success:function(e)
        {
            $('.rezle').html(e);
        }
    })
});

My codes are like this.

oguzunbiri
  • 13
  • 5
  • 2
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Oct 21 '22 at 13:50
  • "it applies to all buttons on the page when I press only one button" - what does that mean? The markup you've shared contains only a single button – Nico Haase Oct 21 '22 at 14:00
  • Also, is this really a PHP problem and not a JS problem? – Nico Haase Oct 21 '22 at 14:01
  • @NicoHaase Clicking a button affects other buttons. yes, js problem. sorry, my english is really bad – oguzunbiri Oct 21 '22 at 14:08
  • "affects other buttons" - that sounds strange. The markup you've shown does not contain any other button – Nico Haase Oct 21 '22 at 14:10

1 Answers1

0

Your problem here is that you're referring to all elements with the class rezle and updating them all with the ajax html response:

$('.rezle').html(e);

try this:

$('.rezle').click(function(){
    const rezle_button = this;
    var soruid = $(this).attr("title");
    
    $.ajax({
        type:"POST",
        url:"rezle.php",
        data:{"soru":soruid},
        success:function(e)
        {
            $(rezle_button).html(e);
        }
    })
});
barneyp
  • 41
  • 3