0

I, don't know what am i doing wrong but i'm trying to use oop with ajax and it's not working well.. i guess. ajax returns success but it doesn't insert and update into database like i need.. here is the code

ajax

 $('.fa-thumbs-o-up').click(function()
{
   $.ajax({
       url:"insert.php",
       type:"post",
       success:function()
       {
           // it returns alert("HEY"); but doesn't insert :X
           alert("Hey");
       }
   });
});

php

 require 'AnswerVoteManager.php';
 require '../config.php';
 if(isset($_POST['upvote']))
 {
  $title = $_GET['title'];
  $answervotemanager = new AnswerVoteManager($dbh);
  $answervotemanager->setVoteType('Upvote');
  $answervotemanager->setAnswerID($_POST['answerID']);
  $answervotemanager->setVoter($_SESSION['username']);
  $answervotemanager->setTitle($title);
  $answervotemanager->vote();
 }

php oop class

 private function addVote()
{
    $sql = "UPDATE question_answers set count = question_answers.count + 1 WHERE id=:id";
    $stmt = $this->dbh->prepare($sql);
    $stmt->bindValue(':id',$this->answerID);
    $stmt->execute();
}
private function subtractVote()
{
    $sql = "UPDATE question_answers set count = question_answers.count - 1 WHERE id=:id";
    $stmt = $this->dbh->prepare($sql);
    $stmt->bindValue(':id',$this->answerID);
    $stmt->execute();
}
private function deleteVote()
{
    $sql = "DELETE FROM answer_votes where answerID=:answerid and username=:username LIMIT 1";
    $stmt = $this->dbh->prepare($sql);
    $stmt->bindValue(':answerid',$this->getAnswerID());
    $stmt->bindValue(':username',$this->voter);
    $stmt->execute();
    $this->subtractVote();
    $this->redirect();
}
private function insertVote()
{
    $sql = "INSERT  into answer_votes (vote_type,answerID,username,questiontitle) values (:vote_type,:answerid,:username,:questiontitle)";
    $stmt = $this->dbh->prepare($sql);
    $stmt->bindValue(':vote_type',$this->getVoteType());
    $stmt->bindValue(':answerid',$this->getAnswerID(),PDO::PARAM_INT);
    $stmt->bindValue(':username',$this->getVoter());
    $stmt->bindValue(':questiontitle',$this->getTitle());
    $stmt->execute();
    $this->addVote();
    $this->redirect();
}
public function vote()
{
   if($this->vote_type == 'Upvote' && $this->voted()) {
       $this->deleteVote();

   } else {
       $this->insertVote();
   }
}
flex_
  • 679
  • 2
  • 8
  • 26
  • 3
    Well for one, you're using `GET` params on a `POST` request, and for a second, you're not passing any parameters in your ajax request. – Sterling Archer Dec 01 '16 at 18:00
  • @SterlingArcher that's correct.. is there anyway i can use get with it too? – flex_ Dec 01 '16 at 18:00
  • what do you mean by passing parameters? i am sorry i am kinda new to ajax.. – flex_ Dec 01 '16 at 18:01
  • http://stackoverflow.com/questions/18697034/how-to-pass-parameters-in-ajax-post read this question for more on ajax params – Sterling Archer Dec 01 '16 at 18:02
  • thanks for the link but is there anyway i can use get with post at the same time? is this issue caused by get or by not passing params? – flex_ Dec 01 '16 at 18:03

0 Answers0