title: Call a PHP File with AJAX
body: <p>I have been looking at other questions and based my code out of the corresponding answers, but I can't seem to make this work. I would appreciate if someone more experienced than me can help me see clearer on this one.</p>
  
  <p><strong>jQuery:</strong></p>
  
  <pre><code>$(function () {
  $(".going-decision").click(function () {
      var clickBtnValue = $(this).val();
      alert('clicked');
      $.ajax({
          type: "POST",
          url: "http://localhost/townbuddies/public_html/event_going_ajax.php",
          data: {'btn-clicked': clickBtnValue},
          success: function () {
              alert('success');
          }
      });
  });
  });
  </code></pre>
  
  <p><strong>event_going_ajax.php:</strong></p>
  
  <pre><code>if (isset($_POST['btn-clicked'])) {
      $decision = $_POST['btn-clicked'];
      //Logic that writes to database
  }
  </code></pre>
  
  <p>My HTML buttons do have the class 'going-decision'.  jQuery library is included.</p>
  
  <p><strong>Result:</strong> The 'clicked' alert is displaying, along with the 'success' one. However, the database is not updated.</p>
  
  <p>Anyone has a hint on what's going on here? Thanks.</p>
  
  <p><strong>EDIT, here's my PHP code. I know it's non-secure code, i'm currently learning PHP.</strong></p>
  
  <pre><code>&lt;?php
  
  session_start();
  $user_id = $_SESSION['user'];
  
  define('DB_HOST', 'localhost');
  define('DB_USER', 'root');
  define('DB_PASSWORD', '');
  define('DB_NAME', 'townbuddies');
  
  $con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  
  if (isset($_POST['btn-clicked'])) {
  //Value will be in this format: 'decision.event_id'
  $value = $_POST['btn-clicked'];
  $length = strlen($value);
  $findme = '.';
  $pos = strpos($value, $findme);
  
  //Separating the decision (going/not going) and the event_id
  $decision = substr($value, 0, $pos);
  $event_id = substr($value, $pos + 1);
  
  //Verify if user is already 'going' to the event
  $result = verifyDatabase($con, $user_id, $event_id);
  
  //Depending on user decision
  switch ($decision) {
      case 'going':
          going($con, $result, $user_id, $event_id);
          break;
      case 'not_going':
          notGoing($con, $result, $user_id, $event_id);
          break;
  }
  }
  
  function verifyDatabase($con, $user_id, $event_id) {
  //Verify if already in database
  $sql = "SELECT * "
          . "FROM user_events "
          . "WHERE event_id = "
          . "'$event_id'"
          . "AND user_id = "
          . "'$user_id'";
  
  $result = mysqli_query($con, $sql);
  
  return $result;
  }
  
  function going($con, $result, $user_id, $event_id) {
  //If already in database as 'going', then do not duplicate. Do nothing.
  if (!empty($result)) {
      header("Location: http://localhost/townbuddies/public_html/event.php?event=" . $event_id);
      exit;
  }
  
  //Not in database as 'going', so add it.
  else {
      $sql = "INSERT INTO user_events (user_id,event_id) "
              . "VALUES ("
              . "'$user_id'"
              . ","
              . "'$event_id'"
              . ")";
  
      if (!mysqli_query($con, $sql)) {
          echo "Error: " . $sql . "&lt;br&gt;" . mysqli_error($con);
      }
  
      //Update amount of people going.
      $sql = 
  
      mysqli_close($con);
  }
  exit;
  }
  
  function notGoing($con, $result, $user_id, $event_id) {
  //If already in database as 'going', then delete
  if (!empty($result)) {
      $sql = "DELETE FROM user_events "
              . "WHERE user_id = "
              . "'$user_id'"
              . "AND event_id = "
              . "'$event_id'";
  
      if (!mysqli_query($con, $sql)) {
          echo "Error: " . $sql . "&lt;br&gt;" . mysqli_error($con);
      }
  
      mysqli_close($con);
  }
  exit;
  }
  
  ?&gt;
  </code></pre>
  
  <p><strong>EDIT#2: FORM</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang = "en"&gt;
  &lt;head&gt;
      &lt;meta http-equiv="content-type" content="text/html; charset=UTF-8"&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;meta name="generator" content="Bootply" /&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"&gt;
      &lt;title&gt;Event&lt;/title&gt;
      &lt;link href = "css/main.css" rel = "stylesheet" /&gt;
      &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"&gt;     &lt;/script&gt;
      &lt;script src="js/event.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;button type = "submit" id = "go-btn" class = "going-decision" value="going.' . $event_id . '"&gt;Going&lt;/button&gt;
      &lt;button type = "submit" id = "no-go-btn" class = "going-decision" value="not_going.' . $event_id . '"&gt;Not Going&lt;/button&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>EDIT#3: Solution</strong></p>
  
  <p>The issue is, indeed, from my PHP script. In the functions going(), and notGoing(), the statement </p>
  
  <pre><code>if (!empty($result))
  </code></pre>
  
  <p>will not work as expected... If I interchange the if and the else statements, everything works fine...</p>
  
  <p>Why would an empty result from mySQL would not be empty for that statement?</p>