0

I have read all the related questions that reference to this topic, but still cannot find answer here.
So, php and ajax works great. The problem starts when i try to include json, between php and ajax, to passing data.
here is my ajax:

function likeButton(commentId, userId, sessionUserId) {

  // check if the comment belong to the session userId
  if(sessionUserId == userId) {
    alert("You cannot like your own comment.");
  }
  else if(sessionUserId != userId) {
    var like_upgrade = false;
    $.ajax({
      url: "requests.php",
      type: "POST",
      dataType: "json",
      data: {
        keyLike: "like",
        commentId: commentId,
        userId: userId,
        sessionUserId: sessionUserId,
        like_upgrade: like_upgrade
      },
      success: function(data) {
        var data = $.parseJSON(data);
        $("#comment_body td").find("#updRow #updComLike[data-id='" +commentId+ "']").html(data.gaming_comment_like);
        if(data.like_upgrade == true) {
        upgradeReputation(userId);
      }
      }
    });
  }
}

Note, that i try not to include this:

var data = $.parseJSON(data);

Also i tried with diferent variable like so:

var response = $.parseJSON(data);

and also tried this format:

var data = jQuery.parseJSON(data);

None of these worked.

here is requests.php file:

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

    $sql_upgrade_like = "SELECT * FROM gaming_comments WHERE gaming_comment_id='$commentId'";
    $result_upgrade_like = mysqli_query($conn, $sql_upgrade_like);
    if($row_upgrade_like = mysqli_fetch_assoc($result_upgrade_like)) {
    $gaming_comment_like = $row_upgrade_like['gaming_comment_like'];
       }
    $gaming_comment_like = $gaming_comment_like + 1;

    $sql_update_like = "UPDATE gaming_comments SET gaming_comment_like='$gaming_comment_like' WHERE gaming_comment_id='$commentId'";
    $result_update_like = mysqli_query($conn, $sql_update_like);

    $sql_insert_like = "INSERT INTO gaming_comment_likes (gaming_comment_id, user_id, user_id_like) VALUES ('$commentId', '$userId', '$sessionUserId')";
    $result_insert_like = mysqli_query($conn, $sql_insert_like);
    $like_upgrade = true;
//json format
    $data = array("gaming_comment_like" => $gaming_comment_like,
     "like_upgrade" => $like_upgrade);
    echo json_encode($data);
    exit();
           }
        }

Note: i also try to include this to the top of my php file:

header('Content-type: json/application');

but still not worked.
What am i missing here?

0

1 Answer 1

4

Don't call $.parseJSON. jQuery does that automatically when you specify dataType: 'json', so data contains the object already.

You should also learn to use parametrized queries instead of substituting variables into the SQL. Your code is vulnerable to SQL injection.

Sign up to request clarification or add additional context in comments.

2 Comments

i'm trying to find a solution for this 2 days and 2 nights! You are absolutely right and it works. Now, i'm waiting 5 minutes until i can accept your answer! thank you!
Yes i have that in mind as for sql injections and i will replace all these with prepare statements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.