title: JS function only returns undefined but alert outputs correct data
body: <p>So I have a Javascript function that is obtaining the first and last name from a PHP file then returning the value. When this Alerts it does produce the correct value but after return and calling this variable it produces undefined:</p>
  
  <pre><code>function getname(username){
    var output="";
  
    $.ajax({
      url: 'getusername.php?username='+username,
      type: "GET",
      dataType: "text",
      success: function(data){
        output += data;
        alert(output);
        return output;
      }
    });
  }
  </code></pre>
  
  <p>So the alert(output) above produces the name: "John Doe" in an alert. Now when I come to call this function in another function:</p>
  
  <pre><code>var username = 'jdoe';
  var uname='';
  uname += getname(username);
  alert("Username: "+username+"\nName: "+uname)
  </code></pre>
  
  <p>This will output Username: jdoe Name: undefined. I have tried multiple ways of doing this but it just seems that the value being returned is undefined when it actually outputs correctly on the alert.</p>
  
  <p>I have tried to define the variables as strings then add the values on thereafter but no luck, what am I missing here? Thanks</p>