mY777
mY777

Reputation: 249

jQuery AJAX call PHP Class function

I wrote an user action analysis but I can't execute my insert into statement. I tried all suggestion on stack can't but figure out my mistake, I don't even get an error: it is running through without executing the statement. I have also an connection php - this works fine so the fault is not there. I would like to insert the action var later but at first I have to call the function. So whats wrong with my request or php function?

jQuery

$( document ).ready(function() {
  var action = '';
  document.addEventListener('click', function(e) {
    e = e || window.event;
    e = e.target || e.srcElement;
    if (e.nodeName === 'SECTION') {
      action = e.id;
      updateAction();
    }
  }, false);

  function updateAction() {
    $.ajax({
      type: "POST",
      url: '../actions/userAction.php',
      data: {action: 'test'},
      success: function(){
        console.log(action);
      }
    });
  }
});

PHP

<?php
class userAction
{
  /* User Action */
  public function updateAction(){
    if(isset($_POST['action']) && !empty($_POST['action'])) {
      $db = getDB();
      $st = $db->prepare("INSERT INTO user_analysis (action) VALUES ('bla') where userID = 1945");
      $st->execute();
      $db = null;
      $_SESSION['uid']=$uid;
      return true;
    }    else
    {
      return false;
    }
  }
}
?>

Upvotes: 1

Views: 3755

Answers (3)

Renjitha22
Renjitha22

Reputation: 213

if(isset($_POST['action']) && !empty($_POST['action'])) {     
  echo $_POST['action'];
}

echo the value of $_POST['action'] inside your if loop...If not getting try to change the - type: "POST", to "GET",

Upvotes: 0

Sebastian Kaczmarek
Sebastian Kaczmarek

Reputation: 8515

Where do you call updateAction() method? I assume you don't. Then why do you expect that it will execute? You have at least 3 ways to deal with it.


1. Make the method static and call it without instantiating a class

If you don't have any reason to construct an object of your class then you can call your method without doing it. Just add this line of code after your class definition:

userAction::updateAction()

This way you call the method which handles your POST request.

2. Instantiate your class and then call your method

The same story as above. The difference here is to construct an object first:

$ua = new userAction();
$ua->updateAction();

3. (the easiest) Get rid of the class and method

As the title suggest. Remove whole code of a class and leave only the body of your method. Like this:

if(isset($_POST['action']) && !empty($_POST['action'])) {
   $db = getDB();
   $st = $db->prepare("INSERT INTO user_analysis (action) VALUES ('bla') where userID = 1945");
   $st->execute();
   $db = null;
   $_SESSION['uid']=$uid;

   return true;
} else {
   return false;
}

This should be the whole content of your file: a condition.

BTW
1. Your query is not valid. If you want to UPDATE a row you need to use UPDATE statement, not INSERT
2. I would suggest you call your class UserAction (uppercase). Just to make it standardised and more intuitive
3. Why do you mix jQuery with VanillaJS? You use jQuery and bind an event using clear JS. Make it semantic and decide if you use jQuery or not.
jQuery way:

var action = '';
$(document).on('click', function(e) {
  e = e || window.event;
  e = e.target || e.srcElement;
  if (e.nodeName === 'SECTION') {
    action = e.id;
    updateAction();
  }
});



That's all. I hope I could help

Upvotes: 1

Milan Chheda
Milan Chheda

Reputation: 8249

You need to use UPDATE instead of INSERT. Try this below code:

UPDATE user_analysis SET action = 'bla' WHERE id = 1945

Upvotes: 0

Related Questions