title: Passing Mysqli result from php-method to html-file to show it up there
body: <p>I wrote a NotesApp, where a user can log in after he registered himself.</p>
  
  <p>I do redirections after every step.</p>
  
  <p>I use a UserID within the table of the notes, to be able to pass back only the notes from the specific user. The UserID is saved in a $_SESSION-Variable within PHP.</p>
  
  <p>The Method in my DBHandler-Class which should get the Data from the database is as follows:</p>
  
  <pre><code>function getNotes($userID){
      assert($this-&gt;connection);
  
      $db = $this-&gt;connection;
  
      $queryCreate = "select `title`,`content` from `notesnew` where `userid` = ?;";
      $statement = $db-&gt;prepare($queryCreate);
      $statement-&gt;bind_param('s',$userID);
      $statement-&gt;execute();
      $result = $statement-&gt;get_result();
      $returnVar = array();
      while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
          $returnVar[] = $row;
      }
      echo json_encode($returnVar);
  }
  </code></pre>
  
  <p>I call the method after the user has pressed the "Add-Note"(he can set title, text then "Add-Note-Button"-> Note is inserted into the database.</p>
  
  <p>Now i want to make an Ajax-Request to get the notes added to the div.</p>
  
  <p>I did some tries, i know that its not completely correct, but i dont know how to pass the $_SESSION["UserID"] (which is actually just on the server-side) within the ajax method.</p>
  
  <p>Thats my first tries of ajax, so dont be mad xD</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="de"&gt;
  &lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;title&gt;MyNotes&lt;/title&gt;
  
  &lt;link rel="stylesheet" href="notesStyle.css"/&gt;
  &lt;script src="http://code.jquery.com/jquery-latest.min.js"
          type="text/javascript"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;form action="notes.php" method="post"&gt;
      &lt;label&gt;Add Note&lt;/label&gt;&lt;br&gt;&lt;br&gt;
      &lt;input type="text" name="Title" value="title"/&gt;&lt;br&gt;
      &lt;input type="text" name="Text" value="text"/&gt;&lt;br&gt;
      &lt;input type="submit" id="Submitter" name="Submit" value="Add Note"/&gt;
  &lt;/form&gt;
  
  &lt;div id="notesDiv"&gt;
      &lt;label&gt;YourNotes&lt;/label&gt;
  &lt;/div&gt;
  
  
  &lt;script&gt;
      $.('#Submitter').click(function(){
          $.ajax({
              url:'DBHandler.php', 
              data:'POST', 
              dataType:'json', 
              success:function(rows){
                  for (var i in rows){
                      var row = rows[i];
                      $('#notesDiv').append(row);
                  }
              }
          });
      });
  &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Is it generally a good way to use Ajax for my specific problem? </p>
  
  <p>Can you tell me what i have to add, to make it working? It also redirects me, after i have clicked on the Add-Note-Button (what i dont want)</p>
  
  <p>I thought about putting the html in a php file and echoing the $_SESSION["UserID"] into a Jscript variable. Does this make sense?</p>
  
  <p>Thanks for your help! Im very glad i signed up here :)</p>