title: Calling a PHP class method and getting the return value using jQuery and AJAX
body: <p>I'm creating a little project for myself. I'm not a very experienced programmer, so I began creating the project in PHP. Soon it became evident that running PHP functions and class methods via HTML button clicks wasn't the most straightforward of tasks.</p>
  
  <p>I have some test code set up. First I created an HTML form, from which I want to read user-input values:</p>
  
  <pre><code>&lt;form name="form" action="" method="POST"&gt;
      Title: &lt;br&gt; &lt;input type="text" id="title"&gt; &lt;br&gt;
      Description: &lt;br&gt; &lt;input type="text" id="description"&gt; &lt;br&gt;
      Priority: &lt;br&gt; &lt;input type="number" id="priority"&gt; &lt;br&gt; &lt;br&gt;
      &lt;input type="button" id="ajaxTest" value="Send&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Now, when the button with id #ajaxTest is clicked, I want to call one of the class methods in a PHP-file that I've created. To this end, I googled around and ended up with the following jQuery:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      $("#ajaxTest").click( function() {
  
          $t = $("#title").val();
  
          $.ajax({
              type: "POST",
              url: "http://www.example.com/task.php",
              data: "t="+t
          }).done(function(results) {
              $("#results").html(results).hide().fadeIn();
          })
      });
  
  &lt;/script&gt;
  </code></pre>
  
  <p>There is also a div-element with id #results in the HTML, to show the resultant data.</p>
  
  <p>Now, the task.php being called looks like as follows (simplified for readibility):</p>
  
  <pre><code>&lt;?php
  class Task {
      private $title;
      private $description;
  
      function SetTitle($t) {
          $this-&gt;title = $t;
      }
  
      function GetTitle() {
          return $this-&gt;title;
      }
  
      function SetDescription($d) {
          $this-&gt;description = $d;
      }
  
      function GetDescription() {
          return $this-&gt;description;
      }
  }
  </code></pre>
  
  <p>How would I go about calling the different member functions of this class via jQuery/AJAX and getting their return values for input to DOM? I take I'll have to create an intermediary class like TaskHandler that initiates the object and so on, but I'm at a complete loss. Been at it almost the whole day, but I've failed to find an answer that would've lead me to a working solution. Ever so thankful for your help!</p>
  
  <p>edit: I have now edited the jQuery and it looks as follows:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
      $("#ajaxTest").click( function() {
  
          var t = $("#title").val();
  
          $.ajax({
              type: "POST",
              url: "http://www.example.com/taskHandler.php",
              data: {"t": t},
              success: function(response) {
                   $("#results").html(response);
              }
          });
      });
  
  &lt;/script&gt;
  </code></pre>
  
  <p>and taskHandler.php is below</p>
  
  <pre><code>&lt;?php
      require_once("task.php");
  
      $t = new Task();
      $t-&gt;SetTitle($_POST['t']);
  
      $output = $t-&gt;GetTitle();
      echo $output;
  </code></pre>
  
  <p>Still don't get anything to show up, though. What's it I'm doing wrong here?</p>