0

I know that this question is already answered a lot, but even the previous responses from php are working, this response cannot work and i cannot find the reason for this issue. Although php was send the response succesfully, ajax cannot display without refreshing the page first. Here is my jquery.ajax code in the file helpers.js:

function likeButton(commentId, userId) {
$.ajax({
  url: "requests.php",
  type: "POST",
  data: {
    like: "likeUp",
    commentId: commentId,
    userId: userId
  },
  success: function(response) {
    $("#comment_body").append(response);
  }
});
  }

Here is my php code in requests.php:

 if(isset($_POST['like'])) {
  if($_POST['like'] == "likeUp") {
    $commentId = $_POST['commentId'];
    $userId = $_POST['userId'];

    $sql = "SELECT gaming_comment_like FROM gaming_comments WHERE gaming_comment_id='$commentId'";
    $result = mysqli_query($conn, $sql);
    if($row = mysqli_fetch_assoc($result)) {
      $gaming_comment_like = $row['gaming_comment_like'];
    }
    $gaming_comment_like = $gaming_comment_like + 1;

    $sql_update = "UPDATE gaming_comments SET gaming_comment_like='$gaming_comment_like' WHERE gaming_comment_id='$commentId'";
    $result_update = mysqli_query($conn, $sql_update);
    exit();
  }
}

here is the eventhandler that calling the likeButton function, which is in a php file:

<p><img src='like.png' class='like_button' onclick='likeButton(".$gaming_comment_id.", ".$user_id.");'>$gaming_comment_like</p>";
Nikos Takiris
  • 149
  • 1
  • 13
  • 3
    Your PHP code does not generate any response for your javascript to display. – rickdenhaan Nov 29 '17 at 18:34
  • What is the actual response you're expecting from this AJAX request? What is the runtime value of the `response` variable in your `success` callback? What were you expecting it to be? Why? – David Nov 29 '17 at 18:35
  • You are returning nothing. So that looks like a fail. – Forbs Nov 29 '17 at 18:35
  • you should print or echo the result of query - `echo json_encode($result_update);` to get the response on js side – Cruzer Nov 29 '17 at 18:35
  • 1
    Your code is vulnerable to [**SQL injection**](https://en.wikipedia.org/wiki/SQL_injection) attacks. You should use prepared statements with bound parameters, via either the [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) drivers. [**This post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) has some good examples. – Alex Howansky Nov 29 '17 at 18:39
  • @David you are right, but this exactly code i used in previous ajax responses and it worked. So what is the solution for that? i tried it to echo to get the response on js side but is not working. – Nikos Takiris Nov 29 '17 at 18:51
  • @NikolasBlues: The "solution" for what, exactly? If your PHP code isn't *creating* any output, then what are you expecting to happen? What are you trying to do? If you want your PHP code to output something then, well *output something*. `echo` something. – David Nov 29 '17 at 18:52
  • i understand your point and you are right David, i'm not output anything. I expected from php to assign +1 to the variable $gaming_comment_like and send this response to ajax. I used this exactly code in a previous ajax-php responses and it worked. Sorry for asking but i don't know what to print from php because i wanted only to assign +1 to the variable that you saw. – Nikos Takiris Nov 29 '17 at 19:05
  • @NikolasBlues: Well, if you want to get the value of `$gaming_comment_like` returned from your AJAX, then `echo $gaming_comment_like;` seems like a reasonable approach. When you say: *"I used this exactly code in a previous ajax-php responses and it worked."* - Well, define "worked". This code *doesn't output anything*. By design. So there's no way "this exact code" ever output anything in another context. – David Nov 29 '17 at 19:14
  • @David yes you are totally right. I echo $gaming_comment_like and now it works. When i said it worked in a previous ajax-php responses i was definetely false, because these previous responses with the exact same code didn't work with ajax, because the displaying of the results was happened in a different page and so after refreshing the page. Thank you David and sorry for my misunderstanding. – Nikos Takiris Nov 29 '17 at 19:49

0 Answers0